4

What does the command $() do in UNIX? Could someone explain what is the significance of $ in the below examples;

$(echo var=10)  

and

eval $(echo var=10)
  • 1
    It was discussed hundreds of times. Please read documentation. // http://mywiki.wooledge.org/CommandSubstitution ; http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03 ; etc – rush Sep 10 '13 at 17:27
  • 1
    In addition to @rush's comment, you can find out that it is called command substitution by searching man bash for “$(”. (Depending on your man pager, you may need to write the search expression as “$\(”.) – manatwork Sep 10 '13 at 17:33
  • 4
    +1 Although there may be lots of already available answers to this question, it's a good question for U&L particularly with $() in the title -- which I don't see in the "Related" sidebar, and that is what people will look for if they don't know it is about command substitution. So thank you for asking that way! – goldilocks Sep 10 '13 at 17:41

1 Answers1

4

This isn't a command itself, rather this is an example of command substitution. It takes the value of what is in parentheses and uses it in context.

For example, I will frequently make a backup of a file before editing it. I'll use command substitution to add the date to the end of the filename in ISO 8601 format (for sorting.) Of course, I could just type the date, but this is great for scripts where the date may be different each time it runs.

$ cp -p /path/to/file /path/to/file.$(date +%F)
$ ls /path/to/file.*
/path/to/file.2013-09-10 /path/to/file