Difference between ${} and $() in bash


The syntax is token-level, so the meaning of the dollar sign depends on the token it’s in. The expression $(command) is a modern synonym for `command` which stands for process substitution; it means, run command and put its output here. So

echo "Today is $(date). A fine day."

will run the date command and include its output in the argument to echo. The parentheses are unrelated to the syntax for running a command in a subshell, although they have something in common (the process substitution also runs in a separate subshell).

By contrast, ${variable} is just a disambiguation mechanism, so you can say ${var}textwhen you mean, the contents of the variable var, followed by text (as opposed to $vartextwhich means, the contents of the variable vartext).

Advertisements

One thought on “Difference between ${} and $() in bash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s