Variables are how programming and scripting languages represent data. They appear in arithmetic operations and manipulation of quantities, in string parsing, and they are indispensable for working in the abstract with symbols -- tokens that represent something else. A variable is nothing more than a label assigned to a location or set of locations in computer memory holding an item of data.
The name of a variable is a placeholder for its value, the data it holds. Referencing its value is called variable substitution.
Let us carefully distinguish between the name of a variable and its value. If variable1 is the name of a variable, then $variable1 is a reference to its value, the data item it contains.
bash$ variable=23 bash$ echo variable variable bash$ echo $variable 23 |
The only time a variable appears "naked" -- without the $ prefix -- is when declared or assigned, when unset, when exported, or in the special case of a variable representing a signal (see Example 29-5). Assignment may be with an = (as in var1=27), in a read statement, and at the head of a loop (for var2 in 1 2 3).
Enclosing a referenced value in double quotes (" ") does not interfere with variable substitution. This is called partial quoting, sometimes referred to as "weak quoting." Using single quotes (' ') causes the variable name to be used literally, and no substitution will take place. This is full quoting, sometimes referred to as "strong quoting." See Chapter 5 for a detailed discussion.
Note that $variable is actually a simplified alternate form of ${variable}. In contexts where the $variable syntax causes an error, the longer form may work (see Section 9.3, below).
Example 4-1. Variable assignment and substitution
1 #!/bin/bash 2 3 # Variables: assignment and substitution 4 5 a=375 6 hello=$a 7 8 #------------------------------------------------------------------------- 9 # No space permitted on either side of = sign when initializing variables. 10 # What happens if there is a space? 11 12 # If "VARIABLE =value", 13 # ^ 14 #+ script tries to run "VARIABLE" command with one argument, "=value". 15 16 # If "VARIABLE= value", 17 # ^ 18 #+ script tries to run "value" command with 19 #+ the environmental variable "VARIABLE" set to "". 20 #------------------------------------------------------------------------- 21 22 23 echo hello # Not a variable reference, just the string "hello". 24 25 echo $hello 26 echo ${hello} # Identical to above. 27 28 echo "$hello" 29 echo "${hello}" 30 31 echo 32 33 hello="A B C D" 34 echo $hello # A B C D 35 echo "$hello" # A B C D 36 # As you see, echo $hello and echo "$hello" give different results. 37 # ^ ^ 38 # Quoting a variable preserves whitespace. 39 40 echo 41 42 echo '$hello' # $hello 43 # ^ ^ 44 # Variable referencing disabled by single quotes, 45 #+ which causes the "$" to be interpreted literally. 46 47 # Notice the effect of different types of quoting. 48 49 50 hello= # Setting it to a null value. 51 echo "\$hello (null value) = $hello" 52 # Note that setting a variable to a null value is not the same as 53 #+ unsetting it, although the end result is the same (see below). 54 55 # -------------------------------------------------------------- 56 57 # It is permissible to set multiple variables on the same line, 58 #+ if separated by white space. 59 # Caution, this may reduce legibility, and may not be portable. 60 61 var1=21 var2=22 var3=$V3 62 echo 63 echo "var1=$var1 var2=$var2 var3=$var3" 64 65 # May cause problems with older versions of "sh". 66 67 # -------------------------------------------------------------- 68 69 echo; echo 70 71 numbers="one two three" 72 # ^ ^ 73 other_numbers="1 2 3" 74 # ^ ^ 75 # If there is whitespace embedded within a variable, 76 #+ then quotes are necessary. 77 echo "numbers = $numbers" 78 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 79 echo 80 81 echo "uninitialized_variable = $uninitialized_variable" 82 # Uninitialized variable has null value (no value at all). 83 uninitialized_variable= # Declaring, but not initializing it -- 84 #+ same as setting it to a null value, as above. 85 echo "uninitialized_variable = $uninitialized_variable" 86 # It still has a null value. 87 88 uninitialized_variable=23 # Set it. 89 unset uninitialized_variable # Unset it. 90 echo "uninitialized_variable = $uninitialized_variable" 91 # It still has a null value. 92 echo 93 94 exit 0 |
![]() | An uninitialized variable has a "null" value - no assigned value at all (not zero!). Using a variable before assigning a value to it will usually cause problems. It is nevertheless possible to perform arithmetic operations on an uninitialized variable.
|