29

What is $() in Linux Shell Commands?

For example:

chmod 777 $(pwd)

3 Answers3

34

It's very similar to the backticks ``.

It's called command substitution (posix specification) and it invokes a subshell. The command in the braces of $() or between the backticks (`…`) is executed in a subshell and the output is then placed in the original command.

Unlike backticks, the $(...) form can be nested. So you can use command substitution inside another substitution.

There are also differences in escaping characters within the substitution. I prefer the $(...) form.

Stephen Kitt
  • 434,908
chaos
  • 48,171
  • 6
    backticks can be nested as well. Note that trailing newline characters are removed from the output of the command. – Stéphane Chazelas Jul 30 '14 at 12:50
  • 2
    @StéphaneChazelas how can backticks be nested? You mean by ecxessive escaping? – chaos Jul 30 '14 at 12:53
  • 3
    Yes: like with echo \echo \`echo foo\`` bar` – Stéphane Chazelas Jul 30 '14 at 12:59
  • 2
    Any official reference saying that backticks are deprecated? – vinc17 Jul 30 '14 at 13:08
  • 1
    Found this answer: http://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated seems they are not deprecated. Changed my answer, thanks – chaos Jul 30 '14 at 13:17
  • 1
    Backticks can be nested as long as you don't care about portability (different shells have different quoting rules). Or about your sanity. – Gilles 'SO- stop being evil' Jul 30 '14 at 23:39
  • @Gilles, would you care to expand on that? Maybe in a Q/A. Which shell(s) don't support the Bourne or POSIX syntax? Or are you referring to the double-quotes that need escaped as well in Bourne/Korn where some others can live without? – Stéphane Chazelas Jul 31 '14 at 08:58
  • @Gilles, OK replying to myself, yash does have trouble with the Bourne/Korn syntax it would seem. And POSIX leaves the behaviour mostly unspecified when nesting with quotes. – Stéphane Chazelas Jul 31 '14 at 09:08
5

In POSIX or POSIX-like shells (ksh, bash, ash, zsh, yash...), it is like ``: the command inside $() is executed and replaced by its standard output. Word-splitting and filename generation are done unless $() is inside double-quotes. Thus

chmod 777 $(pwd)

should be replaced with:

chmod 777 "$(pwd)"

to avoid word-splitting and filename generation on the current working directory path.

Or even better (except under some shells, like zsh, in case the directory has been renamed):

chmod 777 "$PWD"

Since $PWD is a special variable that holds the path to the current working directory in POSIX shells.

Or even better:

chmod 777 .

Since the . entry in the current directory is a hard link to that directory itself.

vinc17
  • 12,174
3

This $() is used for executing a command mostly inside some other command.

chmod 777 $(pwd)

pwd command gives the current working directory. So, when the whole thing is executed output of pwd will replace its position and serve as the argument to chmod , and the result is that all your present working directory get the permission 777 which I guess should never be used in production environment ;) .

beginer
  • 2,708