This lesson is being piloted (Beta version)

Variables and Arrays

Overview

Teaching: 10 min
Exercises: 5 min
Questions
  • How can I store information without writing it to file

Objectives
  • Learn how to create and reference a variable

  • Learn how to create and reference an indexed array

Variables

In the shell novice course you were, indirectly, introduced to bash variables, when working with loops: http://swcarpentry.github.io/shell-novice/05-loop/index.html

for thing in list of things
do
  echo $thing
done
echo 'variables persist:' $thing
list
of
things
variables persist: things

Variables can also be defined directly:

echo $thing
thing=this
echo $thing
things
this

Exploring Shell Variables

You can check if a variable exists, and what it’s contents are, using the declare -p command. This will list all variables and functions in the environment, so to ease use of this output of this command it is recommended that you pipe the output to another command, such as less or grep, to search for the variable you require.

To delete a variable you can use unset.

thing=stuff
unset thing

Note that you do not reference the contents of the variable (using $thing) to do this, instead you reference the variable directly (using thing).

Referencing a non-existent variable

What happens if you reference a variable which doesn’t exist? e.g.

thing=stuff
unset thing
echo $thing

Solution

Referencing a non-existent variable simply returns an empty string, no error message is given. This can aid in the smooth-running of a script, but can also create problems if you are used to the error messages that languages such as python return after referencing a non-existent variable. We will look later at how you can test for the existence of a variables more safely.

Arrays

BASH objects do not have to only contain a single value, they can contain a list of values instead.

To create an array object you can use the notation:

listthings=( these are my things )

Whitespace warning

Note that the whitespace is important — it is used to denote the breaks between individual values, as well as the start and end of the list. If you wish to have items which contain whitespace you will need to wrap these in quotation marks. e.g.

listthings=( these "are my" things )

The array object now contains an indexed list of values:

declare -p | grep -w listthings
listthings=([0]="these" [1]="are my" [2]="things")

To access the contents of the array you should use the indexes, e.g. [0] or [1]. You can also access all values using the index [@]. However, to make use of these indexes you must use curly braces to delimit the variable name.

Referencing Arrays

What are the outputs, and why, from the following commands?

  1. echo ${listthings[1]}
  2. echo $listthings[1]
  3. echo ${listthings[@]}
  4. echo $listthings
  5. echo ${#listthings[@]}

Solution

  1. are my This is the value at index 1
  2. these[1] This is the first (index 0) value in the array, followed by the string [1]
  3. these are my things This is all values within the array.
  4. these This is the first (index 0) value in the array.
  5. 3 This is the length of the array.

Finally, we note that arrays can be referenced using a for loop, as at the start of this lesson:

for thing in ${listthings[@]}
do
  echo $thing
done

This is generally the best way to create a for loop; except for the most trivial examples it is wise to keep the array assignment separate from the loop itself.

Key Points

  • BASH variables can store a single piece of information

  • BASH arrays can store an indexed lists of information

  • {} denotes a code block, and are essential for referencing arrays

  • [@] denotes all of an array, while [X] denotes the value at position X

  • ${#VAR} returns the length of the string

  • ${#ARRAY[@]} returns the number of items in the array