Bash Script: Set variable example

If you are writing a Bash script and have some information that may change during the execution of the script, or that normally changes during subsequent executions, then this should be set as a variable.

Setting a variable in a Bash script allows you to recall that information later in the script, or change it as needed. In the case of integers, you can increment or decrement variables, which is useful for counting loops and other scenarios.

In this tutorial, you will learn how to set variables and use them in a Bash script on a Linux system. Check some of the examples below to see how variables works.

In this tutorial you will learn:

  • How to set a variable in a Bash script
  • How to use a previously set variable
  • How to use a variable inside of another variable
How to set a variable in a Bash script
How to set a variable in a Bash script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to set variable in Bash script




First, let’s go over how setting a variable is done in a Bash script. This will familiarize you with the syntax so you can easily interpret the coming examples, and eventually write your own from scratch.

#!/bin/bash

var="Hello World"

echo $var

Executing the script gives us this output:

$ ./test.sh
Hello World

This is the probably the most basic example of a variable as possible, but it gets the point across. Let’s go over what is happening here:

  • The name of the variable in this example is simply var.
  • The variable is declared by using an equal sign =.
  • The variable is set to "Hello World". The quotes are necessary in this case because of the space.
  • In order to call the variable later in the script, we precede it with a dollar sign $.

Next, look at the examples below to see more practical examples of setting a variable in a Bash script.

Bash Script: Set variable examples

Check out the examples below to see how to set variables within a Bash script.

  1. When you set a variable to execute a command, the command will be executed and the output is stored inside the variable. Here is an example where the current date is stored inside a variable, and then echoed to terminal later. Notice that for this to work, we have to use a Bash subshell with the $( ) syntax, otherwise the command does not get executed.
    #!/bin/bash
    
    date=$(date)
    
    echo $date

    Here is the result from executing the script:

    $ ./test.sh 
    Sat 26 Feb 2022 08:51:19 PM EST
    
  2. The same variable can be declared multiple times in your script. Check out the example below where the $date variable has two different values at different points in the script.
    #!/bin/bash
    
    date=$(date +"%A")
    
    echo "The day of the week is $date"
    
    date=$(date +"%B")
    
    echo "The current month is $date"

    Here is the result from executing the script:

    $ ./test.sh 
    The day of the week is Saturday
    The current month is February
    

    The lesson to take away from this example is that you can re-use a variable inside of a Bash script.

  3. Let’s look at how to prompt the user for a response, and put that data into a variable.
    #!/bin/bash
    
    echo "Enter a directory."
    read directory
    
    number=$(ls -l $directory | wc -l)
    
    echo "There are $number files in $directory"

    Here is the result from executing the script:

    $ ./test.sh 
    Enter a directory.
    /etc
    There are 225 files in /etc
    

    The lesson to take away from this example is that variables are very useful when reading data from the user, whether they specify that data as flags or as a response to a prompt. There is another lesson here too. Notice that when declaring the $number variable, we use the $directory variable as well. In other words, a variable inside of a variable.



Closing Thoughts

In this tutorial, you learned how to set variables and use them in Bash scripting on a Linux system. As you can see from the examples, using variables is incredibly useful and will be a common staple in most Bash scripts. The examples shown here are basic in order to introduce you to the concept, but it is normal for a Bash script to contain many variables.



Comments and Discussions
Linux Forum