Skip to main content
Bash Scripting

Working with BASH Variables

By July 25, 2016September 12th, 2022No Comments

Working with BASH Variables


In this blog we take a look at using and working with BASH variables. We should be able to understand how to read variables and set variables by the end. We also should gain some some knowledge of variable scope. So if you are ready let’s go.

What are Variables

BASH variables are named elements that can store values in memory that can be reused later. They can contain number but cannot begin with a number. The value of a variable can be changed unlike constants whose valuse cannot be changed once set.

Environment Variables

Many variables that we use are often described as “Environment Variables” as they go in some way to set up the user environment. Although variable names are case-sensitive and can be in either upper case or lower case; environment variables are usually all in uppercase.

These include

  • $HOME : The path to your home directory, such as /home/pi
  • $PWD : You current working directory
  • $USER : The logged in user name

When we looked at command line lists in the previous blog we could see how we could use some of these variables:

test $HOME == $PWD && echo "You are home"
#This could also be written as
[ $HOME == $PWD ] && echo "You are home"

Here we use the variable HOME and PWD to read or expand the variable we use the $ symbol so HOME becomes $HOME. When in a quoted string such as we use with the echo command we have to use the double-quotes to be able to expand the variable to its stored value. If we just use single quotes then we will not print the variable’s value we will print the dollar and the variable name.  So for example:

echo "$HOME"

Will print /home/pi or wherever your home directory is; whereas:

echo '$HOME'

Will print $HOME

Double quotes allow variables to be expanded whilst single quotes will print the literal string

As well as the variable $PWD which contains the path to the current directory, BASH maintains a variables $OLDPWD which maintain the last directory we were in. There is a shortcut to this using cd –. This will take us to our last directory.

$ cd /usr/share/doc
$ pwd
/usr/share/doc
$ cd $HOME
$ pwd
/home/pi
$ cd -
$ pwd
/usr/share/doc
Using the command cd – we are taken to the previous directory we were in.

Setting Variables

When we set variables ourselves we often use lowercase characters so we do not clash with environment variables. Variables we set are known as user variables.  We can set a variable using a command like this:

user=bob

This will set a variable named user with a value of bob.  We can read this value back by expanding the variable:

echo "$user"

This should print the value bob to the screen.

We can test if a variable has a value ir not by using the command test with the option -z. To start the process again we can use the unset command to clear the variable as it has never been used.

unset user
#clears the variable so it has no value

test -z $user && echo "The variable $user has no value"
#Here we test if the variable has a value or not. If it has no value then we will see the echo text printed to the screen

user=bob
#We give the variable a value

test -z $user && echo "The variable $user has no value"
#As the variable now does have a value we will not see the message printed to the screen

Variable Scope

BASH Variables also have something called scope. By default the scope is local to the shell that the variable was defined in. So its value can only be read from that shell. If you start another shell the variable will not have the value that was set on the parent shell:

unset user
#clears the variable so it has no value

user=bob
#We give the variable a value

test -z $user && echo "The variable $user has no value"
#As the variable now does have a value we will NOT see the message printed to the screen

bash
#We now start a new shell. A sub-shell to the shell in which we set the variable $user

test -z $user $$ echo "The variable $user has no value"
#Now we DO see the message as the variable has no value in the new shell

If we export the variable then it is available to sub-shells. This is normally done with variables defined in a login script to ensure the value is available to all sub-shells:

export user=bob

Where the variable is exported in the way the values is maintained in sub-shells.

unset user
#clears the variable so it has no value

export user=bob
#We give the variable a value and export the variable

test -z $user && echo "The variable $user has no value"
#As the variable now does have a value we will NOT see the message printed to the screen

bash
#We now start a new shell. A sub-shell to the shell in which we set the variable $user

test -z $user $$ echo "The variable $user has no value"
#As the value has been exported we again still see no message as the variable has a value

The video steps you through the process or using and setting BASH Variables.


My Book on Shell Scripting is available from good book shops and the publisher Packt.

shellscripting