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).

一个有关“Difference between ${} and $() in bash”的想法


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

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 登出 /  更改 )

Google photo

您正在使用您的 Google 账号评论。 登出 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 登出 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 登出 /  更改 )

Connecting to %s