4

This question already has an answer here:

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)

marked as duplicate by don_crissti, Jakuje, Scott, garethTheRed, Jeff Schaller Mar 17 '16 at 21:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    It was discussed hundreds of times. Please read documentation. // mywiki.wooledge.org/CommandSubstitution ; pubs.opengroup.org/onlinepubs/9699919799/utilities/… ; 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
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
  • This $(echo something) was always written `echo something` before, and you easily overlook those backticks, especially on a cheap printer. – ott-- Sep 10 '13 at 20:40
  • 5
    Not to mention the nightmare it creates for nesting! – Aaron Copley Sep 10 '13 at 21:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.