assign output of shell command to variable in bash
January 31st, 2007 by Lawrence David
to let the variable ‘j’ equal the output of a shell command, such as find, try:
j=$(find `echo $i | sed ‘s/AnGST\/.*/AnGST\//’` -name “*.seq”)
basically, it looks like all you need to do is enclose the command with a dollar-sign and some parentheses. now, your shell scripts won’t need to be populated by unending lines of pipes.

It’s funny that you’ve mixed idioms in your example. You’ve used the preferred $() syntax to enclose a backticked command:
j=$(find $(echo $i | sed ’s/AnGST\/.*/AnGST\//’) -name “*.seqâ€)
would be more consistent. The primary reason $() is preferred to backticks is because they nest easily:
j=`ls` and j=$(ls) are equivalent, but try writing your example with just backticks.
Plus, sed (or perl) does not need to use the slash to delimit its substitutions, it can use any other character if you want to use the slash in your matches. You’ll find it’s common for programmers to use the “#” symbol when trying to match forward slashes, so your example would become:
j=$(find $(echo $i | sed ’s#AnGST/.*#AnGST/#’) -name “*.seqâ€)
which I find more readable than the backslashed version, but that could just be me.
Finally, depending you what your $i’s look like, you may be able to replace $( echo $i | sed ‘…’ ) with $( dirname $i ), but I’m just inferring that from the example, and may not exactly fit the bill.
I’ll shut up now.
Your example doesn’t work.
Unrecognized command: s/AnGST/.*/AnGST//
find: illegal option — n
find: [-H | -L] path-list predicate-list
eric: this is really late, but much thanks for all of those tips!! i’m amazed at how much unix i’ve learned by sharing what little i know about bash!
josh: don’t try and run what’s within $(). instead, replace what i’ve got with your own commands.
So I googled “bash assign output to variable” and this was the first site. I’m a novice Unix admin, and was looking for a way to automate some things from the command line. This saved me some headache. Thank you!
Not sure what’s right or wrong — but hey your example worked perfectly for me … my first bash script works
Thanks
Haha one of the first links to come up when you google “bash assign variable”
Thanks for this nugget, really helps with makefile scripting!
I love your site, it has just saved me hours of work with this little tid bit of information
Thank you for this! It just saved me a bunch of time!
Thanks for your tips. It works for me. I am using GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu).
I just wanna thank you for telling this way to assign output of shell command to variable ,It works well and helps alot.
That worked for me, thanks a lot for sharing this !!
That worked for me, thanks a lot for sharing this !!
I am trying to add the result of a shell in the same way but is not working,
BINDIREXISTS:=$(shell if test -d $(TOP)/$(1); then echo “T”; else echo “F”; fi )
@Aldo
looks like gnumake syntax, that being said, I dunno what you have $(TOP) defined as, or what $(1) could even be.
generally you can do something like if [[ -d BINDIR ]]; then echo “T”; else echo “F”; fi
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html#sect_07_02_01
Thanks for the tip.
Bless you
saved me a few hours
even the smallest amount of knowledge shared can be useful to others who know lesser still
Thanks
Hey,thanks for the tip!
Thanks for the ‘$(foo bar)’
I had a great little script to find the current working directory, but I couldn’t get it to fill a variable
echo “${PWD}” | awk -F’/’ ‘{print $NF}’ #worked fine
but I want to use this in a script so the following worked great.
ParentDir=$(echo “${PWD}” | awk -F’/’ ‘{print $NF}’)
Thanks again.
Wolf: a better way to do your bit is use commands “basename” resp. “dirname”
.
ParentDir=$(basename ${PWD})
But calling this ParentDir is a bit misleading – in fact, it’s the last section of the path provided, either a file or directory – I think the function name “basename” is pretty self explanatory…
thanks.