Bash How to get Result of Command
To get the result of the command you need to use command substitution (bash feature). Command substitution provides executing a bash command/commands and store its output to a variable.
You can use special backticks (“). Everything what you write between backticks is executed by the shell before the main command is executed. Sometimes people call them back quotes. Surely you know that they’re under [Esc] key.
In this example, we get date into variables:
DATE=`date -I` echo $DATE
If you expect multi-line output, and you would like to display it, it is good practice to use double quotes. In this example, we get all files in /etc into variable FILES:
FILES=$(ls /etc -l) echo "$FILES"
What if you want to use backticks into backticks? It is a little trouble. The solution is to use a dollar with brackets $(). This example does the same:
DATE=$(date -I) echo $DATE
It is also possible to use the inner way. In the next example, the output of date will be used as the first echo argument:
echo `date -I`