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 in computer memory holding an item of data.
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$ variable1=23 bash$ echo variable1 variable1 bash$ echo $variable1 23 |
The only times a variable appears "naked" -- without the $ prefix -- is when declared or assigned, when unset, when exported, in an arithmetic expression within double parentheses (( ... )), or in the special case of a variable representing a signal (see Example 32-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 10.2, 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 #------------------------------------------------------------------------- 11 # No space permitted on either side of = sign when initializing variables. 12 # What happens if there is a space? 13 14 # "VARIABLE =value" 15 # ^ 16 #% Script tries to run "VARIABLE" command with one argument, "=value". 17 18 # "VARIABLE= value" 19 # ^ 20 #% Script tries to run "value" command with 21 #+ the environmental variable "VARIABLE" set to "". 22 #------------------------------------------------------------------------- 23 24 25 echo hello # hello 26 # Not a variable reference, just the string "hello" ... 27 28 echo $hello # 375 29 # ^ This *is* a variable reference. 30 echo ${hello} # 375 31 # Likewise a variable reference, as above. 32 33 # Quoting . . . 34 echo "$hello" # 375 35 echo "${hello}" # 375 36 37 echo 38 39 hello="A B C D" 40 echo $hello # A B C D 41 echo "$hello" # A B C D 42 # As we see, echo $hello and echo "$hello" give different results. 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" # $hello (null value) = 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 legacy 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 # uninitialized_variable = 106 # It still has a null value. 107 echo 108 109 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. |