Chapter 4. Introduction to Variables and Parameters

Variables are at the heart of every programming and scripting language. They appear in arithmetic operations and manipulation of quantities, string parsing, and are indispensable for working in the abstract with symbols - tokens that represent something else. A variable is nothing more than a location or set of locations in computer memory holding an item of data.

4.1. Variable Substitution

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. 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 30-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 
  11 #  If "VARIABLE =value",
  12 #+ script tries to run "VARIABLE" command with one argument, "=value".
  13 
  14 #  If "VARIABLE= value",
  15 #+ script tries to run "value" command with
  16 #+ the environmental variable "VARIABLE" set to "".
  17 #-------------------------------------------------------------------------
  18 
  19 
  20 echo hello    # Not a variable reference, just the string "hello".
  21 
  22 echo $hello
  23 echo ${hello} # Identical to above.
  24 
  25 echo "$hello"
  26 echo "${hello}"
  27 
  28 echo
  29 
  30 hello="A B  C   D"
  31 echo $hello   # A B C D
  32 echo "$hello" # A B  C   D
  33 # As you see, echo $hello   and   echo "$hello"   give different results.
  34 # Quoting a variable preserves whitespace.
  35 
  36 echo
  37 
  38 echo '$hello'  # $hello
  39 #  Variable referencing disabled by single quotes,
  40 #+ which causes the "$" to be interpreted literally.
  41 
  42 # Notice the effect of different types of quoting.
  43 
  44 
  45 hello=    # Setting it to a null value.
  46 echo "\$hello (null value) = $hello"
  47 #  Note that setting a variable to a null value is not the same as
  48 #+ unsetting it, although the end result is the same (see below).
  49 
  50 # --------------------------------------------------------------
  51 
  52 #  It is permissible to set multiple variables on the same line,
  53 #+ if separated by white space.
  54 #  Caution, this may reduce legibility, and may not be portable.
  55 
  56 var1=variable1  var2=variable2  var3=variable3
  57 echo
  58 echo "var1=$var1   var2=$var2  var3=$var3"
  59 
  60 # May cause problems with older versions of "sh".
  61 
  62 # --------------------------------------------------------------
  63 
  64 echo; echo
  65 
  66 numbers="one two three"
  67 other_numbers="1 2 3"
  68 # If whitespace within a variable, then quotes necessary.
  69 echo "numbers = $numbers"
  70 echo "other_numbers = $other_numbers"   # other_numbers = 1 2 3
  71 echo
  72 
  73 echo "uninitialized_variable = $uninitialized_variable"
  74 # Uninitialized variable has null value (no value at all).
  75 uninitialized_variable=   #  Declaring, but not initializing it
  76                           #+ (same as setting it to a null value, as above).
  77 echo "uninitialized_variable = $uninitialized_variable"
  78                           # It still has a null value.
  79 
  80 uninitialized_variable=23       # Set it.
  81 unset uninitialized_variable    # Unset it.
  82 echo "uninitialized_variable = $uninitialized_variable"
  83                                 # It still has a null value.
  84 
  85 echo
  86 
  87 exit 0

Caution

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.
   1 echo "$uninitialized"                                # (blank line)
   2 let "uninitialized += 5"                             # Add 5 to it.
   3 echo "$uninitialized"                                # 5
   4 
   5 #  Conclusion:
   6 #  An uninitialized variable has no value, however
   7 #+ it acts as if it were 0 in an arithmetic operation.
   8 #  This is undocumented (and probably non-portable) behavior.
See also Example 11-20.