#!/bin/bash# Naked variablesecho# When is a variable "naked", i.e., lacking the '$' in front?# When it is being assigned, rather than referenced.# Assignmenta=879echo"The value of \"a\" is $a."# Assignment using 'let'leta=16+5echo"The value of \"a\" is now $a."echo# In a 'for' loop (really, a type of disguised assignment):echo-n"Values of \"a\" in the loop are: "for a in78911doecho-n"$a "doneechoecho# In a 'read' statement (also a type of assignment):echo-n"Enter \"a\" "readaecho"The value of \"a\" is now $a."echoexit0
Example 4-3. 變數的賦值,單純與花式
#!/bin/basha=23# Simple caseecho $ab=$aecho $b# Now, getting a little bit fancier (command substitution).a=`echoHello!`# Assigns result of 'echo' command to 'a' ...echo $a# Note that including an exclamation mark (!) within a#+ command substitution construct will not work from the command-line,#+ since this triggers the Bash "history mechanism."# Inside a script, however, the history functions are disabled by default.a=`ls-l`# Assigns result of 'ls -l' command to 'a'echo $a # Unquoted, however, it removes tabs and newlines.echoecho"$a"# The quoted variable preserves whitespace.# (See the chapter on "Quoting.")exit0
使用$(...) 的方式給變數賦值(比使用 `` 還新的方式)這就是第十二章的指令替代方式。
# From /etc/rc.d/rc.localR=$(cat/etc/redhat-release)arch=$(uname-m)