In this article, I will go through different methods using which you can set/assign output of a Linux Command to a variable. If you are frequently using Bash Scripting then you might know or aware of the methods used to set/assign output of a Linux command to a variable but in case you are not then don't be afraid, we will see all the methods in great detail with examples in below section. But before that we need to understand what is variable in Linux ? Well, the definition of variable in Linux is not very different from what you already know from other Scripting or programming Languages.
In Linux also, variable is a string which is used temporarily store some values. The value assigned could be a number, text, filename, device, or any other type of data. But the way variables are used in Linux are little different from what you already know from other programming languages. In Linux, variables are broadly divided into two types - System defined variables and User defined variables.
System defined variables are the one which are predefined and generally managed and maintained by the Linux OS itself. For example - BASH, LOGNAME, OSTYPE, PWD, BASH_VERSION etc. These type of variables are available system wide and can be called and used from any place in Linux. You can use commands like set, env and printenv to check all the System defined variables.
The other set of variables are known as User defined variables which are managed and maintained by the User itself. In this types of variables, generally you don't need to define the data type of a variable and you also don't need to declare it before hand. These type of variables are mostly locally used and can be accessed only from within the terminal session you are currently working on. You can check more about variable assignment on Advanced Bash-Scripting Guide web page.
How to Set/Assign Output of a Linux Command to a Variable
Method 1: Using variable=$(command)
The first method that you can use to assign the output of a command to a variable using $(dollar) operator. You can put the command under dollar parenthesis and then assign it to variable like var in this case to store the output of the command. For example, here we are assigning the output of
date +%s command to a variable called var using
var=$(date +%s) from terminal as shown below. Then we are checking variable value using echo $var command to see if the command output is indeed getting stored or hold by the variable. So looks it does, hence this is one perfect and easy method that can be used to store or hold the command output.
root@localhost:~# var=$(date +%s) root@localhost:~# echo $var 1636533860
You can also verify the output of variable by running the command manually and see if it shows the correct value.
root@localhost:~# date +%s 1636533868
Method 2: Using variable=`command`
This method is also not very much different from the previous one. It is just that instead of dollar and parenthesis you need to use
single quote(` `) operator to store the command value to a variable. To show an example, we are using the previous command
date +%s under single quote and then assigning this to a variable called
var=`date +%s` command. Then displaying the value stored in the variable using
echo $var command as shown below.
root@localhost:~# var=`(date +%s)` root@localhost:~# echo $var 1636534041
Like previous method, you can also verify here if the variable is showing correct value by running the command manually.
root@localhost:~# date +%s 1636534050