Bash: Assign Output of Shell Command To Variable

by on April 24, 2012 · 12 comments· LAST UPDATED October 1, 2013

in , ,

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?

You need to use command substitution feature of bash. It allows you to run a shell command and store its output to a variable. To assign output of any shell command to variable in bash, use the following command substitution syntax:
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsBash
Estimated completion time2m

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

OR

 
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

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

 
now=$(date)
 

OR

 
now=`date`
 

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

echo "$now"

OR

printf "%s\n" "$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"

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)"

OR

printf "Today is %s\n" "$(date)"

Sample outputs:

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

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 12 comments… read them below or add one }

1 John Eisenhower April 25, 2012 at 10:56 pm

They might want to add it .bash_profile so it is persistent across reboots. Such as;

$ cat ~/.bash_profile
alias ll=’ls -l’
alias now=’date +%H:%M’
$ now
18:55

Reply

2 Chris F.A. Johnson May 4, 2012 at 5:31 pm

Error: printf “%s\n” now

Should be: printf “%s\n” “$now”

Reply

3 nixCraft May 7, 2012 at 2:27 pm

Thanks for the heads up!

Reply

4 Amit June 12, 2013 at 12:02 pm

i m trying to kill a process running on port say 4723…by using command “kill `lsof -t -i:4723`”…..it works fine..but i need to pass variable in lsof command instead of giving directly 4723…like..

var number = 4723;
“kill `lsof -t -i:number`”
is it possible…? and how..plz replyyy soon..thanks in advance

Amit

Reply

5 Ahmed Samir July 19, 2013 at 1:59 pm

this is very helpful thank you

Reply

6 Charly August 11, 2013 at 6:55 am

What about Piped command like
this?
ie.
droppedbyte=(iptables -L -v -n –line-number |grep DROP| awk ‘$1 = 5 {print $3}’)
or
droppedbyte=`iptables -L -v -n –line-number |grep DROP| awk ‘$1 = 5 {print $3}’`
Fail :(

-bash: syntax error near unexpected token `|’

This isn’t working because i used this “|”, need help

Reply

7 nixCraft August 11, 2013 at 7:41 am

Try

droppedbyte="$(iptables -L -v -n --line-number | grep DROP | awk '$1 = 5 {print $3}')"
echo "$droppedbyte"

Reply

8 Charly August 11, 2013 at 8:28 am

you are awesome :D

This also worked.
droppedbyte=`iptables -L -v -n –line-number |grep DROP| awk ‘$1 = 5 {print $3}’`

Reply

9 Vimjob October 30, 2013 at 3:41 am

Total shot in the dark Google search landed me here and this thing worked! Thanks a TON!

Reply

10 MattS September 30, 2013 at 10:04 pm

Thanks nixCraft — You just saved me a lot of time.

Reply

11 vignesh January 23, 2014 at 5:59 pm

Hi,

Am stuck in a shell script that stores a value recrusively when it runs to a variable.
when am running manually the value get stores but in crontab its not getting store.
please help me in this, below is my script.

#!/bin/ksh
rm /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/out.csv
a=`ls -lad /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/Archive | head -1 | awk -F" " '{print $5}'`
#echo "$a" >> /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/out.csv
#a=`cat size.txt | head -1 | tail -1 | awk -F"|" '{print $1}'`
b=`$c`
echo "$a,$c" >> /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/out.csv
if [ "$a" = "$b" ]
then
 echo "Meridio Interface stuck=$a" >> /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/output.csv
 cat /u01/oracle/sba81/nas_siebel_fs_prd1/Interfaces/Meridio/Outbound/output.csv | mailx -s "Meridio Interface Down" email1 email2 email3
 c=`$a`
else
 c=`$a`
fi

Reply

12 romain June 20, 2014 at 11:03 am

Simple, concise, straighforward… excellent tutorial !

Many thanks!

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: