Chapter 4. Introduction to Variables and Parameters

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.

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.

 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 #  "VARIABLE =value"
  13 #           ^
  14 #% Script tries to run "VARIABLE" command with one argument, "=value".
  15 
  16 #  "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 
  41 echo
  42 
  43 echo '$hello'  # $hello
  44 #    ^      ^
  45 #  Variable referencing disabled by single quotes,
  46 #+ which causes the "$" to be interpreted literally.
  47 
  48 # Notice the effect of different types of quoting.
  49 
  50 
  51 hello=    # Setting it to a null value.
  52 echo "\$hello (null value) = $hello"
  53 #  Note that setting a variable to a null value is not the same as
  54 #+ unsetting it, although the end result is the same (see below).
  55 
  56 # --------------------------------------------------------------
  57 
  58 #  It is permissible to set multiple variables on the same line,
  59 #+ if separated by white space.
  60 #  Caution, this may reduce legibility, and may not be portable.
  61 
  62 var1=21  var2=22  var3=$V3
  63 echo
  64 echo "var1=$var1   var2=$var2   var3=$var3"
  65 
  66 # May cause problems with older versions of "sh" . . .
  67 
  68 # --------------------------------------------------------------
  69 
  70 echo; echo
  71 
  72 numbers="one two three"
  73 #           ^   ^
  74 other_numbers="1 2 3"
  75 #               ^ ^
  76 #  If there is whitespace embedded within a variable,
  77 #+ then quotes are necessary.
  78 #  other_numbers=1 2 3                  # Gives an error message.
  79 echo "numbers = $numbers"
  80 echo "other_numbers = $other_numbers"   # other_numbers = 1 2 3
  81 #  Escaping the whitespace also works.
  82 mixed_bag=2\ ---\ Whatever
  83 #           ^    ^ Space after escape (\).
  84 
  85 echo "$mixed_bag"         # 2 --- Whatever
  86 
  87 echo; echo
  88 
  89 echo "uninitialized_variable = $uninitialized_variable"
  90 # Uninitialized variable has null value (no value at all).
  91 uninitialized_variable=   #  Declaring, but not initializing it --
  92                           #+ same as setting it to a null value, as above.
  93 echo "uninitialized_variable = $uninitialized_variable"
  94                           # It still has a null value.
  95 
  96 uninitialized_variable=23       # Set it.
  97 unset uninitialized_variable    # Unset it.
  98 echo "uninitialized_variable = $uninitialized_variable"
  99                                 # It still has a null value.
 100 echo
 101 
 102 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,
   7 #+ however it acts as if it were 0 in an arithmetic operation.
   8 #  This is undocumented (and probably non-portable) behavior.
See also Example 14-22.