Marie-Hélène Burle

Variables are a convenient way to assign a value to a name.

Declaring variables

You declare a variable (i.e. a name that holds a value) with the = sign:


Make sure not to put spaces around the equal sign.



You can delete a variable with:

unset var

Expanding variables

To expand a variable (to access its value), you need to prepend its name with $.

This is not what we want:

echo var

This however works:

echo $var


When declaring

Quotes are necessary for values containing special characters such as spaces.

This doesn’t work:

var=string with spaces
echo $var
bash: line 1: with: command not found

This works:

var="string with spaces"
echo $var
string with spaces

This also works:

var='string with spaces'
echo $var
string with spaces

When declaring variables, single and double quotes are equivalent. Which one should you use then? Use the one that is most convenient.

Not good:

var='that's a string with spaces'
echo $var
bash: -c: line 1: unexpected EOF while looking for matching `''
bash: -c: line 3: syntax error: unexpected end of file


var="that's a string with spaces"
echo $var
that's a string with spaces

Alternatively, single quotes can be escaped, but it is a little crazy: the first ' ends the first string, both " create a double-quoted string with ' (escaped) in it, then the last ' starts the second string.

var='that'"'"'s a string with spaces'
echo $var
that's a string with spaces

Conversely, this is not good:

var="he said: "string with spaces""
echo $var
bash: line 1: with: command not found

While this works:

var='he said: "string with spaces"'
echo $var
he said: "string with spaces"

Double quotes as well can be escaped (simply by prepending them with \):

var="he said: \"string with spaces\""
echo $var
he said: "string with spaces"

When expanding

While not necessary in many situations, it is safer to expand variables in double quotes, in case the expansion leads to problematic special characters. In the example above, this was not problematic and using $var or "$var" are the same.

In the following example however, it is problematic:

var="string with spaces"
touch $var

This creates 3 files called string, with, and spaces. Probably not what you want.

The following creates a single file called string with spaces:

var="string with spaces"
touch "$var"

To be safe, it is thus a good habit to quote expanded variables.

It is important to note however that single quotes don’t expand variables (only double quotes do).

The following would thus create a file called $var:

var="string with spaces"
touch '$var'

Exporting variables

Using export ensures that all inherited processes of this shell also have access to this variable:

export var=3

String manipulation

Getting a subset

echo ${var:2}      # Print from character 2
echo ${var:2:1}    # Print 1 character from character 2

Search and replace

echo ${var/l/L}    # Replace the first match of l by L
echo ${var//l/L}   # Replace all matches of l by L

String concatenation

If you want to concatenate the expanded variable with another string, you need to use curly braces or quotes.

This does not return anything because there is no variable called varshine:

echo $varshine

These two syntaxes do work:

echo ${var}shine
echo "$var"shine

Environment variables

Environment variables help control the behaviour of processes on a machine. You can think of them as customizations of your system.

Many are set automatically.


echo $HOME

There are many other environment variables (e.g. PATH, PWD, PS1). To see the list, you can run printenv or env.

If you want to add new environment variables, you can add them to your ~/.bashrc file which is sourced each time you start a new shell.

Here is a video of a previous version of this workshop.