Skip to main content

Learn Bash Scripting

Chapter #3: How to Use and Declare Variables in Bash Scripts

In this guide, we will explore Bash scripting variables in depth, including how to use them effectively, along with essential types and techniques for working with variables.

Shell scripting, like other scripting languages, has all the features and capabilities that make it a super powerful language for writing scripts.

One of the important concepts of programming is the use of variables. All programming languages use variables, with just a difference in how we define and use them.

In this guide, we will dive deep into variables in Bash scripting, how we can use them, and some types and techniques that we should know when dealing with variables.

What is a Variable?

You can think of a variable as a space or a box to store some information and use it later. This makes our code organized and reusable. You define a name and assign it a value, and whenever you need that value, you only need to type its name.

So, the takeaway from this is that each variable should have a name and a value:

How Variable Work

Declare a Variable in Bash

The variable declaration is the phase where we define the name and the value of the variable. We often call this operation initialization.

In Bash, to declare a variable, we use the following syntax:

myvar="pro.tecmint.com"

In the example above, we declare a variable with the name myvar and the value pro.tecmint.com, and in between them, we put the symbol = without any spaces.

So, whenever we need the value pro.tecmint.com, we only call the variable. What you also need to know is that the variable name should follow some rules that we need to keep in mind when creating a variable.

In Bash scripting, the variable name should not start with the following:

  • Special characters: It is not allowed to start a variable name with characters like @,#,-,!.
  • Numbers: The first character in a variable name should not be a number. Example: 1var="tecmint" is invalid.
  • Spaces: In Bash, spaces in the variable name are not allowed.
  • Reserved keywords: Do not use reserved keywords as variable names. Example: if, function, etc.

Just remember what you should not use. Other characters are allowed, and you can use them when naming your variables.

Now, let's put what we learned into an example. Inside the directory that we created, add a new file and call it, for example, variable.sh, then add the following content:

#!/bin/bash

myvar="pro.tecmint.com"

The first line might be new to you. What does it do? This line tells Linux that the file is a Bash script. The #! is called a 'shebang,' and it’s followed by the location where Bash is installed, which is /bin/bash.

You can check this by running the command:

which bash

Output:

/usr/bin/bash

The second line is where the variable declaration is.

Reference a Variable in Bash

At this point, we are done with the first step; we define our variable with a value. Next, how can we call and use it?

In Bash, to call a defined variable, we use the symbol $ before the name of the variable, like this:

#!/bin/bash

myvar="pro.tecmint.com"

echo $myvar

We use the command echo, which prints text to the screen, and then we pass the variable name that we need to print.

In this example, it is $myvar, and the result when we run the script is:

Printing Variables with Echo in Bash

The dollar sign ($) tells Bash that the following name is a variable, not normal text. If you use myvar without the symbol, it will be treated as plain text, not a variable.

Let's do another example like this:

#!/bin/bash

myvar="pro.tecmint.com"

echo "The website name is $myvar"

If we run this script, we will get this:

Dollar Sign in Bash Variables

In Bash, if you use double quotes and put a variable inside, it will output the value of the variable, not the name.

In case you use single quotes instead, it will print the name of the variable, not the value:

#!/bin/bash

myvar="pro.tecmint.com"

echo 'The website name is $myvar'

Using Single Quotes for Variables in Bash

So remember, whenever you need the content of a variable to be printed with the echo statement, use double quotes.

Now you know how variables are defined and executed, but you may have a question, why do we actually need a variable?

Variables have many use cases and benefits. One of them is to avoid retyping the same thing multiple times.

For example:

echo "Learn Linux with Pro.Tecmint"
echo "Pro.Tecmint is great"
echo "Use Pro.Tecmint to learn Bash scripting"

Now you know what this script does; it basically prints those messages to the screen. If you pay attention, the word Pro.Tecmint is repeated in all sentences.

In this situation, to avoid repeating the word many times, we can just use a variable instead, like this:

name="Pro.Tecmint"
echo "Learn Linux with $name"
echo "$name is great"
echo "Use $name to learn Bash scripting"

The output of this example will be:

Use Variables to Reduce Repetition in Bash

Here, we use a variable with the name name and the value Pro.Tecmint to avoid repeating the same word multiple times.

Another useful thing is that if we need to modify the word Pro.Tecmint , for example, to TecMint.com, we only need to modify the variable, and it will be automatically updated throughout the script. We don’t need to modify it in all places.

Storing Command Output in a Variable

Another useful scenario where you will need variables in your script is to capture the output of a command.

For instance, let's say you need to get the output of the pwd command and store it in a variable. The pwd command just prints the working directory where you are in the terminal. We can do it like this:

wk=$(pwd)
echo $wk

In the example above, we declare a variable called wk and store within it the value of the pwd command. We use the syntax with a dollar sign and parentheses - inside the parentheses, we put the shell command that we need to capture. Don't worry about the syntax; we will explain what this does later in detail.

Now, if we run this, we should get the following result:

Storing Command Output in a Variable

The output will be different depending on where you run the script. As we said before, pwd stands for "print working directory," so it will print the directory where you are.

Another example of this task, and a useful one, is capturing the date and time inside a variable and printing it like this:

time=$(date)
echo "Time now is $time"

In this example, we define a variable time, and it stores the output of the date command, which shows the current time and date.

So, if you run this, you will get an output with the system date and time like this:

Capturing Date and Time in Bash

Using Environment Variables in Bash

Another term that we need to know when we work with the shell is the concept of environment variables. Basically, these are variables defined by the system that we have access to, and we can use them in our scripts.

In Linux, these variables are always in uppercase, while the simple variables that we define are in lowercase - this way, we can distinguish between them.

Let's do an example:

time=$(date)
echo "Time now is $time"
echo "The username logged into this system is $USER"

We understand the first two lines; we already learned what they do, but the third line has something new. We use the variable USER without declaring it first. Well, this is an environment variable, and it is already defined, so we can use it directly.

πŸ’‘
Note that it is in uppercase.

Now, if we run this, we should get something like this:

Using Predefined Environment Variables in Bash

It will show the date and time, and then it will output the username.

Environment variables in Bash are variables that are defined by the system, such as HOME, USER, PWD, etc. These are predefined, so you don’t need to assign them a value.

In Linux, you can view the environment variables available on your system using the command:

printenv
List Environment Variables

It is common practice to create user-defined variable names in lowercase, so if someone sees an uppercase variable, they will know that it is an environment variable.

Summary

In this guide, we've covered the basics of variables in Bash scripting, which are essential for organizing and managing data in your scripts. We learned how to declare, reference, and manipulate variables, and how they can make your code more efficient and readable.

Next, in the next chapter, we will dive deeper into other characteristics and tricks for using variables.