Enter your email address below and subscribe to our newsletter

Bash for Dummies #2: Why Variables?

Learn how to use variables in bash script. You will learn how to use environment variables in the script and manipulation of data.

Share your love

In the previous part, I went through how you can execute a basic bash script but with variables, you get a lot of control over your bash scripts.

Open your terminal and execute the given line to announce a variable in your terminal:

myname="Kabir"

Here, I have declared a variable called myname which contains a text Kabir. But why did I do that in the first place? Well, now whenever I need to use Kabir in the terminal, I can print the value of the variable.

To print the value of the variable, you can use the echo command along with the variable name starting with a dollar ($) sign as shown here:

echo $variable-name
Define and print variable in bash

But hey, this is not the most practical use of variables as these are just temporary variables and will be wiped out once you close the terminal.

The real use of variables starts with the script. So let’s take a look at how you can use variables in bash script.

How to use variables in bash script

First, open your terminal and create a new file using nano called variable.sh using the given command:

nano variable.sh

Now, let’s declare some variables here. I’ll be using two variables which will have my name and age and then print the value of the variable using the echo command:

#!/bin/bash

name="Kabir"
age="27"

echo "Hello everyone, my name is $name."
echo "I'm pretty young and my age is $age."

If it does not make sense, hold on and paste the lines in the nano editor, save changes and exit from the nano editor.

Finally, make a file executable using the chmod command and execute. The output would look like this:

Use variables in bash script

Here are two things that you need to take note of.

  • How I used the value of variables in a sentence: When you want to include the value of a variable in a printed sentence using the echo command, enclose the entire sentence in double quotes (” “). This allows the shell to interpret and expand variables within the string.
  • The syntax I used to print the variables: To declare a variable in the shell, use the format variable_name="variable_value". Note that there should be no spaces around the equals sign. When you want to use the value of a variable, prefix the variable name with a dollar sign ($).

Let’s take another (and more practical) example.

Using one variable multiple times

I find myself using one variable many times in a script so in future, if I wanted to change the meaning of a script, all I had to do is change the value of a variable.

Open your terminal again and create a new script called greetings.sh:

nano greetings.sh

Now, paste the following lines of code in the file:

#!/bin/bash

greeting="Hello"

echo "$greeting, world!"
echo "I said $greeting!"
echo "Why don't you say $greeting back?"
echo "Is $greeting too formal?"
echo "Maybe we should stick with $greeting for now."
echo "Anyway, $greeting and welcome to our script!"

Now, save the file, exit from the editor and run the script. The output should look like this:

Using one variable multiple times in bash script

As you can see, I have used the greeting variable multiple times which contained the word “Hello” and that is the reason why you will see the use of “Hello” multiple times.

Now, if I want to change the entire meeting of the output, all I have to do is change the value of the greeting variable.

For example, here, I have changed the value of the greeting variable from Hello to Sorry:

#!/bin/bash

greeting="Sorry" #Changed the value of the greeting variable to Sorry

echo "$greeting, world!"
echo "I said $greeting!"
echo "Why don't you say $greeting back?"
echo "Is $greeting too formal?"
echo "Maybe we should stick with $greeting for now."
echo "Anyway, $greeting and welcome to our script!"

Now, if I execute the same script, the meaning of the output will look entirely different:

Change the value of variable

How to use command output in bash script

From here, you are going to witness the real use of the bash scripts as I’ve written so many scripts and most of them use command output one or the other way.

First, let me show you how you can do it in the terminal window and later on will move to the script.

To use command output as a variable, you need to follow the given syntax:

variable_name=$(command)

For example, if I want to store the output of the ls command in the list variable, then I will use the following:

list=$(ls)

Now, I can use the echo command to print the value of the list variable and it should print the output of the list command:

echo $list
Use command output as variable in bash

Now you know the syntax, let’s take it to the script. Create a new file called command.sh:

nano command.sh

Next, paste the following lines into the file (will explain in a moment):

#!/bin/bash

name="Kabir Thapar"
now=$(date)

echo "Hello $name"
echo "The system date and time is:"
echo $now
echo "Logged-in username is: $USER"

Finally, save the file and exit from the nano editor. Make sure to make the file executable and run the script. The output would look like this:

Running a script where variables are used to store command output

Here I did two things differently:

  • I stored the date command’s output in the now variable and later on I used the echo command to print the value of the now variable.
  • In the end, I used the USER variable, which I didn’t even declare and still, it printed my name. How? I used an environment variable.

Environment variables are default variables available in Linux which store critical information about a system and for the most part, they are in all capitals so that the user can differentiate between a variable created by the user and an environment variable.

Also, there are hundreds of environment variables available in Linux. You can list available environment variables using the env command:

env
List environment variables in Linux

Next: Learn how to use if-else in Bash

In the next part of this series, I will explain how you can use an if-else condition statement by which you can control the flow of the script and make correct decisions.

If you like such tutorials, we would appreciate a share with someone who is trying to learn the bash script.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!