How to Store Command Output to Bash Variable? [3 Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Efficiency is an essential criterion of practical shell scripting, and a fundamental aspect of this is the skill of storing command output into a variable to use later. Whether you are an advanced or novice Bash programmer, mastering this technique is essential. This article will explore the process to store command output to the Bash variable. So without further delay, let’s start!

Syntax to Store Command Output to Variables in Bash Script

When you execute a command, you can print the output on the terminal, or you can save the output to a variable in Bash Script. To store the command output in a variable, use the following syntax:

var=$(COMMAND)
var=$(COMMAND ARGUMENT1)

Alternatively, you can use the backtick(`) to do the same task. Basic syntax using backtick is given below

var=`COMMAND`
var=`COMMAND ARGUMENT1`

I am going to use the first type of syntax throughout this article.

3 Practical Examples to Store Command Output to Variables in Bash Scripts

While writing Bash script, programmers might need to save the output of a command into a variable. In this section, I will show you three practical examples related to this. The first one is about saving the output of the date command, and the second example is related to modifying the value of a variable using the cut command. And finally, in the third example, multiline command output is saved to a variable.

1. Set Variable to the Output of a Command

You can easily set the output of a command on a variable and then print the value of that variable on the terminal. Here, I will save the output of the date command on the current_date variable and then print the variable value on the terminal.

To set a variable to the output of a command, follow the below script:

#! /bin/bash

#setting variable
current_date=$(date)

#printing the value of the variable
echo "Today is $current_date"
EXPLANATION

#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then the output of the date command is saved on the current_date variable using the dollar ($) symbol. Then echo command prints the value of the current_date variable on the terminal.

The bash script has saved the output of the date command to a variable and then printed the value of the variable on the terminal.The image shows that the bash script has saved the output of the date command to a variable and then printed the value of the variable on the terminal.

2. Store Output of a Command With Options and Arguments to a Variable

Suppose you have a file named weekdays.txt containing the name of 7 days of a week. Now you want to print the first three characters of a day, and for a specific day, you can do it by using the cut command with the -c option and keeping the three characters into a variable.

Here’s a text file containing names of 7 days of a week:

Monday 
Tuesday 
Wednesday 
Thursday 
Friday 
Saturday 
Sunday

Now, to store output of a command with options and arguments to a variable, check the below script:

#!/bin/bash

#assigning the filename to a variable
filename='weekday.txt'

#whole loop will read each line from the file and assign to line variable
while read line; do

#first three character from each line will be stored on the day variable
day=`echo $line | cut -c 1-3`
if [ $day == "Sun" ]
then
#if the day variable is “Sun” then it will print a specific time
echo "Sunday is the holiday"

else
#otherwise, it will print the value of the day variable
echo $day
fi
done<$filename
EXPLANATION

The filename variable will store the name of the file containing the names of the week days. Then the while loop will read each line from the weekday.txt and store it in the line variable. Afterward, the first three characters from the line variables will be kept on the day variable. If the day variable value is “Sun“, the “Sunday is the holiday” will be printed on the terminal. Otherwise, it will print the value of the day variable.

The Bash script has stored the first three characters from all the variables and printed them on the terminal, and for Sunday, it has printed a special message.The image shows that the Bash script has stored the first three characters from all the variables and printed them on the terminal, and for Sunday, it has printed a special message.

3. Store Multiline Command Output to a Variable Using Bash Script

When a command length gets bigger, you can write it in multiline, then after executing it, save the output to a variable. Here, I have developed a script to overcome such a case.

To store multiline command output to a variable, follow the below script:

#!/bin/bash

#output of date command with the proper option is saved to the OUT variable
OUT=$(date \
--date='TZ="America/Washington" 09:00 next Thu')

#the value of the OUT variable is printed on the terminal
echo "$OUT"
EXPLANATION

The output of the multiline command “date command with the America/Washington option” is saved to the OUT variable. Then the value of the OUT variable is printed on the terminal with the echo command.

The Bash script has stored the output of the date command with option and argument on the OUT variable and then printed the value of the OUT variable to the terminal.The image shows that the Bash script has stored the output of the date command with option and argument on the OUT variable and then printed the value of the OUT variable to the terminal.

Conclusion

In conclusion, learning to save the output to Bash variables is an essential skill for efficient scripting because programmers can run a lengthy command with options and arguments stored inside a single variable. This capability empowers developers to integrate command output dynamically, enhancing script functionality. With proper documentation and practice, mastering this skill is a significant step toward becoming a proficient Bash developer.

People Also Ask

How to store output in a variable in Bash?

You can easily store the output of a command into a variable in Bash by following the below syntax: var=$(command), var=$(command argument).

How do I redirect standard output to a variable in bash?

In Bash, you can store the standard error output of a command to a variable by using the ‘2>&1operator and the ‘$()command substitution syntax. Here ‘2>’ redirects the error message to &1`, which represents to standard output. In the case of bash shell works as the standard output device.

How do I capture output in Bash?

In Bash, to capture the output of a command and store it in a variable, you can use $() for performing command substitution. Suppose, to capture the output of the ls command in a variable named result, the syntax will be: result=$(ls).

What is the use of variable in shell?

Variables are useful to store data and information within a shell, and they are also used for controlling the behavior of programs and scripts.

How do I set variables in Bash?

The easiest way to set variables in Bash is to use the variable name, an equal sign, and the value you want to assign to the variable.


Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment