How to Store or Assign a Linux command in a Variable in Bash Shell Script

You can store Linux commands as variables in shell scripts by using the syntax: VARIABLE_NAME=$(COMMAND_TO_BE_EXECUTED).

Once you have saved the Linux command as a variable in the shell script, you can use it anywhere in the script by calling the variable name instead of typing out the command.

For example, you can use the variable to store a command that prints out the custom date format and then use it whenever needed in the script by just assigning the variable name.

But how can we use these variables to execute different commands within the script? The answer is simple: we can use the command substitution feature and the variables to execute commands inside the script.

So let’s take a look at how we can leverage command substitution to execute commands within the script and fetch the output of those commands into the variables.

What is Command Substitution?

Command substitution allows us to store a command in a variable and then execute it within the script by simply referring to the variable name, and this can be achieved by using a special syntax, which looks like this:

variable_name=$(command)
variable_name=$(command [option ...] args1 args2 ...)
OR
variable_name=`command`
variable_name=`command [option ...] args1 args2 ...`

With this syntax, you can save a command’s output in a variable and use it later on in the script.

After becoming aware of the syntax, let’s take a look at some examples to understand how it works.

Examples

To demonstrate this syntax, we can look at an example where we print out the custom time format in hour, minute, and second and, after its successful execution, run the same command in short script.

$ now=$('date' '+%H:%M:%S')
$ echo "Time is now $now"

You can see that the command is functioning as intended from the output in the section below.

The output of the above command
The output of the above command

Also See: Comprehensive guide on date command usage in Linux

Now create a script file with any of your favorite text editors and paste the following line of text into it, then run the file with the bash filename.sh command.

Script result
Script result

You can achieve the same result by using backtick, but for readability it is not recommended to use it as it makes the code harder to understand.

#! /bin/bash 
now=`'date' '+%H:%M:%S'`
echo "Time is now $now"

To add some complexity to the variable data, you could pipe the results of any command into another command and store the result in a variable.

For example, if you want to make the ls command output into the grep command to get a total count of all the html files in the directory, you can use the following command syntax:

$ files_count=$(ls /home/shen/html\ files | grep -c ".html") 

To call the variable, you can simply reference the variable “file_count,” like so:

$ echo "Total number of html file in this following directory is $files_count"

If you want, you can also save variable data in multiline format by enclosing the data in single or double quotes and separating each line with a backslash character.

#! /bin/bash
files_count=$(ls /home/shen/html\ files/|\
grep ".html"|\
wc -l)

echo "Total number of html file in this following directory is $files_count"

You can also save variable data in an array and access it using index numbers, so to demonstrate, I have created a basic script file that will loop through and list out all files that end with the “.html” extension by removing white space between filenames using the sed command.

#! /bin/bash
files_count=$(ls /home/shen/html\ files/| grep ".html" | sed  's/\s\+/-/g')

files_array=($files_count)

for i in ${!files_array[@]}; do
  echo "On element $i is ${files_array[$i]}"
done

Here is the output of the above script:

Iterate result to print array data
Iterate result to print array data

Wrap up

This concludes our discussion of how to store or assign a Linux command as a variable in a bash shell script. If you have any questions or comments, please feel free to leave them in the comments section below.

Till then, put your feet up and relax. As always, the next article will be up shortly.

Leave a Reply