Variables are how programming and scripting languages represent data. A variable is nothing more than a label, a name assigned to a location or set of locations holding an item of data, in computer memory.
Variables appear in arithmetic operations and manipulation of quantities, and in string parsing.
The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) 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. [1]
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 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 # ex9.sh 3 4 # Variables: assignment and substitution 5 6 a=375 7 hello=$a 8 9 #------------------------------------------------------------------------- 10 # No space permitted on either side of = sign when initializing variables. 11 # What happens if there is a space? 12 13 # "VARIABLE =value" 14 # ^ 15 #% Script tries to run "VARIABLE" command with one argument, "=value". 16 17 # "VARIABLE= value" 18 # ^ 19 #% Script tries to run "value" command with 20 #+ the environmental variable "VARIABLE" set to "". 21 #------------------------------------------------------------------------- 22 23 24 echo hello # hello 25 # Not a variable reference, just the string "hello" . . . 26 27 echo $hello # 375 28 # ^ This *is* a variable reference. 29 echo ${hello} # 375 30 # Also a variable reference, as above. 31 32 # Quoting . . . 33 echo "$hello" # 375 34 echo "${hello}" # 375 35 36 echo 37 38 hello="A B C D" 39 echo $hello # A B C D 40 echo "$hello" # A B C D 41 # As you see, echo $hello and echo "$hello" give different results. 42 # Why? 43 # ======================================= 44 # Quoting a variable preserves whitespace. 45 # ======================================= 46 47 echo 48 49 echo '$hello' # $hello 50 # ^ ^ 51 # Variable referencing disabled (escaped) by single quotes, 52 #+ which causes the "$" to be interpreted literally. 53 54 # Notice the effect of different types of quoting. 55 56 57 hello= # Setting it to a null value. 58 echo "\$hello (null value) = $hello" 59 # Note that setting a variable to a null value is not the same as 60 #+ unsetting it, although the end result is the same (see below). 61 62 # -------------------------------------------------------------- 63 64 # It is permissible to set multiple variables on the same line, 65 #+ if separated by white space. 66 # Caution, this may reduce legibility, and may not be portable. 67 68 var1=21 var2=22 var3=$V3 69 echo 70 echo "var1=$var1 var2=$var2 var3=$var3" 71 72 # May cause problems with older versions of "sh" . . . 73 74 # -------------------------------------------------------------- 75 76 echo; echo 77 78 numbers="one two three" 79 # ^ ^ 80 other_numbers="1 2 3" 81 # ^ ^ 82 # If there is whitespace embedded within a variable, 83 #+ then quotes are necessary. 84 # other_numbers=1 2 3 # Gives an error message. 85 echo "numbers = $numbers" 86 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 87 # Escaping the whitespace also works. 88 mixed_bag=2\ ---\ Whatever 89 # ^ ^ Space after escape (\). 90 91 echo "$mixed_bag" # 2 --- Whatever 92 93 echo; echo 94 95 echo "uninitialized_variable = $uninitialized_variable" 96 # Uninitialized variable has null value (no value at all!). 97 uninitialized_variable= # Declaring, but not initializing it -- 98 #+ same as setting it to a null value, as above. 99 echo "uninitialized_variable = $uninitialized_variable" 100 # It still has a null value. 101 102 uninitialized_variable=23 # Set it. 103 unset uninitialized_variable # Unset it. 104 echo "uninitialized_variable = $uninitialized_variable" 105 # It still has a null value. 106 echo 107 108 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 may cause problems. It is nevertheless possible to perform arithmetic operations on an uninitialized variable.
|
[1] | Technically, the name of a variable is called an lvalue, meaning that it appears on the left side of an assignment statment, as in VARIABLE=23. A variable's value is an rvalue, meaning that it appears on the right side of an assignment statement, as in VAR2=$VARIABLE. A variable's name is, in fact, a reference, a pointer to the memory location(s) where the actual data associated with that variable is kept. |