Bash Script Variables

A variable is nothing more than a name given to a particular area in memory that stores a value created by Bash Script. Variable names in Bash Script can contain letters, numbers and underscores, but must start with either a letter or underscore. Variable names are also case sensitive and by convention in uppercase. Unlike some languages, for example C#, the datatype does not need to be specified when a variable is declared. A variable is automatically converted to the correct datatype depending on the value that is assigned to it.

When creating a variable, it takes the following format. The equals sign between the variable name and value is known as an assignment operator and will be discussed further in the next section on operators. It is important to omit any spacing between the variable name and equals sign, as well as the equals sign and the value. Where a string value is being stored in the variable, quotes are only necessary where spaces are included in the string.

VARIABLE_NAME=value

Below is an example of a variable called 'PNAME' being set to the value 'Fred'.

PNAME=Fred

When a variable needs to be used, its name must be proceeded by a dollar sign, '$'. Below, the variable 'PNAME' is displayed in the terminal.

PNAME=Fred

echo $PNAME

The value of a variable can also be displayed as part of a string.

PNAME=Fred

echo "Hello $PNAME"

Here, the message "Hello Fred" is displayed in the terminal.

Where the variable has other text immediately preceding or following it, the name must be enclosed in curly braces.

PNAME=Fred
BIRTHDAY="6 March"

echo "${PNAME}'s birthday is on ${BIRTHDAY}."

This results in the following being displayed in the terminal.

Fred's birthday is on 6 March.

Below are some example variables, the last of which is a Boolean variable, that can either be ‘true’ or ‘false’.

# Integer variable.
EXAMPLE_INTEGER=5

# Floating point or decimal number variable.
EXAMPLE_FLOAT=5.0

# String variable.
EXAMPLE_STRING="This is a string"

# Boolean variable.
EXAMPLE_BOOLEAN=true

One thing to note is that variable names must not be the same as reserved words in Bash Script. Reserved words have a specific meaning and cannot be used for anything else. They are shown in blue on the preceding and following pages.

System Variables

As well as user defined variables, discussed above, there are also some that are defined by the system. These are listed below.

  • $0 - The name of the Bash Script.
  • $1 - $9 - The first 9 arguments to the Bash Script.
  • $# - How many arguments were passed to the Bash Script.
  • $@ - All the arguments supplied to the Bash Script.
  • $? - The exit status of the most recently run process.
  • $$ - The process ID of the current script.
  • $USER - The username of the user running the script.
  • $HOSTNAME - The hostname of the machine the script is running on.
  • $SECONDS - The number of seconds since the script was started.
  • $RANDOM - Returns a different random number each time it is referred to.
  • $LINENO - Returns the current line number in the Bash Script.