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)
3
  • 1
    It was discussed hundreds of times. Please read documentation. // mywiki.wooledge.org/CommandSubstitution ; pubs.opengroup.org/onlinepubs/9699919799/utilities/… ; etc Commented Sep 10, 2013 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 “\$\(”.) Commented Sep 10, 2013 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! Commented Sep 10, 2013 at 17:41

1 Answer 1

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
2
  • This $(echo something) was always written `echo something` before, and you easily overlook those backticks, especially on a cheap printer. Commented Sep 10, 2013 at 20:40
  • 5
    Not to mention the nightmare it creates for nesting! Commented Sep 10, 2013 at 21:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.