var=5Variables
You can assign values to names. These names and the values they hold are called “variables”.
Variables are a convenient way to reuse values.
Declaring variables
You declare a variable (i.e. a name that holds a value) with the = sign:
var=valueMake sure not to put spaces around the equal sign.
Example:
You can delete a variable with the unset command:
unset varExpanding variables
To expand a variable (to access its value), you need to prepend its name with $.
This is not what we want:
var=value
echo varvar
This however works:
var=value
echo $varvalue
Quotes
When declaring
Quotes are necessary for values containing special characters such as spaces.
This doesn’t work:
var=string with spaces
echo $varbash: line 1: with: command not found
This works:
var="string with spaces"
echo $varstring with spaces
This also works:
var='string with spaces'
echo $varstring with spaces
When declaring variables, single and double quotes are equivalent. Which one should you use then? Use the one that is most convenient.
This is not good:
var='that's a string with spaces'
echo $varbash: -c: line 1: unexpected EOF while looking for matching `''
This works well:
var="that's a string with spaces"
echo $varthat's a string with spaces
Alternatively, single quotes can be escaped, but it is a little crazy: the first ' ends the first string, then the apostrophe needs to be escaped (\'), finally, the third ' starts the second string.
var='that'\''s a string with spaces'
echo $varthat's a string with spaces
Conversely, this is not good:
var="he said: "string with spaces""
echo $varbash: line 1: with: command not found
While this works:
var='he said: "string with spaces"'
echo $varhe said: "string with spaces"
Double quotes as well can be escaped (simply by prepending them with \):
var="he said: \"string with spaces\""
echo $varhe 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" both work.
In the following example however, it is problematic:
var="string with spaces"
touch $varThis creates 3 files called string, with, and spaces. Probably not what you wanted…
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 the current shell also have access to this variable:
Example:
var=3
zsh # Launch Zsh (another shell)
echo $varThis returns nothing: var is not defined in the Zsh process.
export var=3
zsh
echo $varThis returns 3: var got exported into the Zsh process.
String manipulation
Getting a subset
var="hello"
echo ${var:2} # Print from character 2
echo ${var:2:1} # Print 1 character from character 2llo
l
Bash indexes from 0.
Search and replace
var="hello"
echo ${var/l/L} # Replace the first match of l by L
echo ${var//l/L} # Replace all matches of l by LheLlo
heLLo
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:
var=sun
echo $varshineThese two syntaxes do work:
var=sun
echo ${var}shine
echo "$var"shinesunshine
sunshine
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.
Example:
echo $HOME/home/user09
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.