Bash Assign Output of Shell Command To Variable

ADVERTISEMENTS



Bash Assign Output of Shell Command To And Store To a Variable

How do I assign the output of a shell command to a shell variable under Unix like operating system? For example, I want to store the date command output to a variable called $now. How do you do that? How can I set a variable to the output of a command in Bash?You need to use command substitution feature of bash. It allows you to run a shell command and store its output to a bash variable.

To assign output of any shell command to variable in bash, use the following command substitution syntax:

Tutorial details Difficulty Easy (rss) Root privileges No Requirements Bash Time 2m

var =$ ( command-name-here ) var =$ ( command-name-here arg1 ) var =$ ( / path / to / command ) var =$ ( / path / to / command arg1 arg2 ) var=$(command-name-here) var=$(command-name-here arg1) var=$(/path/to/command) var=$(/path/to/command arg1 arg2)

OR use backticks based syntax as follows to assign output of a Linux command to a variable:

var = ` command-name-here ` var = ` command-name-here arg1 ` var = `/ path / to / command ` var = `/ path / to / command arg1 arg2 ` var=`command-name-here` var=`command-name-here arg1` var=`/path/to/command` var=`/path/to/command arg1 arg2`

Do not put any spaces after the equals sign and command must be on right side of = . See how to assign values to shell variables for more information.

Examples

Let us see some common examples for Linux, macOS, *BSD and Unix-like systems running bash.

BASH command output to the variable

To store date command output to a variable called now , enter:

## store date command output to $now ## now =$ ( date ) ## store date command output to $now ## now=$(date)

OR

## alternate syntax ## now = ` date ` ## alternate syntax ## now=`date`

To display back result (or output stored in a variable called $now) use the echo or printf command:

echo " $now " printf "%s

" " $now " echo "$now" printf "%s

" "$now"

Sample outputs:

Wed Apr 25 00:55:45 IST 2012

You can combine the echo command and shell variables as follows:

echo "Today is $now " echo "Today is $now"

Sample outputs:

Today is Wed Apr 25 00:55:45 IST 2012

You can do command substitution in an echo command itself (no need to use shell variable):

echo "Today is $(date) " printf "Today is %s

" " $(date) " echo "Today is $(date)" printf "Today is %s

" "$(date)"

Sample outputs:

Today is Wed Apr 25 00:57:58 IST 2011

How to use multiline command

Try the following syntax:

my_var=$(command \

arg1 \

arg2 \

arg3 )

echo "$my_var"

For example,

## date example ## OUT =$ ( date \ --date = 'TZ="America/Los_Angeles" 09:00 next Thu' ) echo " $OUT " ## date example ## OUT=$(date \ --date='TZ="America/Los_Angeles" 09:00 next Thu') echo "$OUT"

Another ping command example for you:

#!/bin/bash _ping = "/bin/ping" domain = "www.cyberciti.biz" ping_avg = "$( ${_ping} \ -q \ -c 4 \ ${domain} | grep rtt)" echo "Avg ping time for ${domain} : ${ping_avg} " #!/bin/bash _ping="/bin/ping" domain="www.cyberciti.biz" ping_avg="$(${_ping} \ -q \ -c 4 \ ${domain} | grep rtt)" echo "Avg ping time for ${domain} : ${ping_avg}"

Conclusion

You learned how to assign output of a Linux and Unix command to a bash shell variable. For more information see GNU bash command man page here and read the following docs:

Command substitution – from the Linux shell scripting tutorial wiki.

See man pages: printf(1)