Feed on

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.

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

20 Responses to “assign output of shell command to variable in bash”

  1. on 01 Oct 2007 at 2:41 pm Eric Brunson

    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.

  2. on 07 Apr 2008 at 9:00 pm Josh

    Your example doesn’t work.

    Unrecognized command: s/AnGST/.*/AnGST//
    find: illegal option — n
    find: [-H | -L] path-list predicate-list

  3. on 08 Apr 2008 at 8:11 am Lawrence David

    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.

  4. on 25 Jun 2008 at 4:57 pm Itch

    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!

  5. on 02 Aug 2008 at 9:30 am Jacques

    Not sure what’s right or wrong — but hey your example worked perfectly for me … my first bash script works

    Thanks :-)

  6. on 19 Apr 2010 at 9:21 am Andrew L

    Haha one of the first links to come up when you google “bash assign variable”

    Thanks for this nugget, really helps with makefile scripting!

  7. on 03 Aug 2010 at 5:03 am nicholas king

    I love your site, it has just saved me hours of work with this little tid bit of information :-)

  8. on 16 Aug 2010 at 4:27 pm UncleNinja

    Thank you for this! It just saved me a bunch of time! :D

  9. on 25 Aug 2010 at 10:13 pm Xiaolei Chen

    Thanks for your tips. It works for me. I am using GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu).

  10. on 02 Sep 2010 at 2:36 am zhengjiachao

    I just wanna thank you for telling this way to assign output of shell command to variable ,It works well and helps alot. :)

  11. on 14 Sep 2010 at 8:58 am neha

    That worked for me, thanks a lot for sharing this !! :)

  12. on 14 Sep 2010 at 8:59 am neha

    That worked for me, thanks a lot for sharing this !!

  13. on 07 Oct 2010 at 3:53 pm Aldo Camargo

    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 )

  14. on 07 Oct 2010 at 4:08 pm Andrew

    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


  15. on 10 Oct 2010 at 9:20 am Mario

    Thanks for the tip.

  16. on 28 Mar 2011 at 12:21 am K Y Iyer

    Bless you
    saved me a few hours
    even the smallest amount of knowledge shared can be useful to others who know lesser still


  17. on 19 Jun 2011 at 4:58 am Saar

    Hey,thanks for the tip!

  18. on 21 Nov 2011 at 12:50 pm Wolf Halton

    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.

  19. on 23 Mar 2012 at 8:52 am cepal

    Wolf: a better way to do your bit is use commands “basename” resp. “dirname” :-D .

    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…

  20. on 08 May 2012 at 9:06 am sey

    thanks. :)

Did I get this wrong? Let me know!

Trackback URI | Comments RSS