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:
Alternatively, you can use the backtick(`) to do the same task. Basic syntax using backtick is given below
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:
#printing the value of the variable
echo "Today is $current_date"
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:
Now, to store output of a command with options and arguments to a variable, check the below script:
#assigning the filename to a variable
#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" ]
#if the day variable is “Sun” then it will print a specific time
echo "Sunday is the holiday"
#otherwise, it will print the value of the day variable
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:
#output of date command with the proper option is saved to the OUT variable
--date='TZ="America/Washington" 09:00 next Thu')
#the value of the OUT variable is printed on 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.
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:
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>&1’ operator 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:
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.
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]