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 advance 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!
Key Takeaways
- Learning about the process of storing the output of a command to a variable.
- Knowing the process to set the output of multiline commands to a variable.
Free Downloads
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.
Example 01: 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 know more, follow the below script.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano script1.sh
❸ Copy the script mentioned below:
#! /bin/bash
#setting variable
current_date=$(date)
#printing the value of the variable
echo "Today is $current_date"
#! /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.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod u+x script1.sh
❻ Run the script by the following command:
./script1.sh
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.
Example 02: 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. To know more, follow the below script.
You can follow the steps of example 01, to save and make the file executable.
Text File (weekday.txt) >
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Script (script2.sh) >
#!/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
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.
Run the script by the following command:
./script2.sh
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.
Example 03: 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 know more, follow the below script.
You can follow the steps of example 01, to save and make the file executable.
Script (script3.sh) >
#!/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"
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.
Run the script by the following command:
./script3.sh
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.