Skip main navigation

Variables and Their Scope

Learn more about variables in shell scripting.

Bash variables provide temporary storage for information. You can use them to store words/phrases (strings), decimals or integers.

Variables

To assign a variable, we use the = symbol:

name="Victoria"

 

BE CAREFUL! When you assign a value to a variable, there should not be any spaces on either side of the = symbol.

 

When we want to access a variable, we need to use the $ symbol to reference it:

 

echo $name
Victoria

 

Because our variables may contain whitespace which gets interpreted by bash, it’s good practice to wrap the variable name in curly brackets and encase it in double quotes:

 

echo "${name}"

 

We can add variables to our Bash scripts, for example:

 

#!/usr/bin/env bash
name="Victoria"
greeting="Good morning,"
 
echo "${greeting} ${name}"

 

When you run this script, you should see:

 

Good morning, Victoria

 

In Bash, variables don’t have to be declared. What we mean by this is that if you try to access a variable that doesn’t exist, you won’t see an error, just a blank value.

 

For example, a script containing the following commands:

 

#!/usr/bin/env bash
 
var2="foo"
 
echo "Variable 1 value: ${var1}"
echo "Variable 2 value: ${var2}"

 

Would return only the value of var2 because var1 had not been declared; Bash just ignored it.

 

Variable 1 value:
Variable 2 value: foo

 

Variable scope

 

There are two types of variable you need to be aware of:

 

 

    • Local variables

 

    • Global variables

 

 

Local variables

 

Local variables are only accessible within the section of code in which they are declared. For example, if we declare a variable inside our Bash script, it will not be accessible outside of that script.

 

Let’s reuse our earlier script:

 

 
#!/usr/bin/env bash
 
var2="foo"
 
echo "Variable 1 value: ${var1}"
echo "Variable 2 value: ${var2}"

 

Now, we know when we run our script we can see the value of var2:

 

Variable 1 value:
Variable 2 value: foo

 

However, what happens if we call var2 outside of the script, directly in our terminal?

 

echo "Variable 2 value: ${var2}"

 

This time we get:

 

Variable 2 value:

 

As you can see, the scope of our variable was constrained to the script itself and its value is not accessible outside of that script.

 

The same principle is true for functions, which we will look at later in the week.

 

Global variables

 

In Week 1 we introduced you to global variables, also known as environment variables, that are available to all shells. You can recall global variables within your Bash scripts.

 

Let’s print our current working directory by recalling the value of the global variable, PWD from within our script:

 

#!/usr/bin/env bash
 
echo "${PWD}"

 

This will likely return a different path for you but, will look something like:

 

/Users/tory

 

You can create your own global variables by using the export command. First let’s declare a variable that contains our name:

 

MY_NAME="Victoria"

 

Next, let’s create a script that tries to access this variable:

 

#!/usr/bin/env bash
 
echo "My name is: ${MY_NAME}"

 

If we were to run this script, we wouldn’t see our name as the MY_NAME variable we created was only a local variable and therefore not accessible within our script.

 

My name is:

 

However, if we use the export command, we can declare MY_NAME as a global variable which is accessible in our script:

 

export MY_NAME="Victoria"

 

Our script would then output:

 

My name is: Victoria
© Wellcme Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now