Here we evaluate a command using two approaches. This executes a subshell which does not have access to the outer env variables and runs in another process.

Base syntax

$(EXPRESSION)

Don’t confuse with the maths evaluation syntax:

$ echo $((1 + 3))
4

Backticks versus brackets

Using backticks is legacy syntax. Don’t do this.

$ X=`which ruby`

Using brackets like $() is more modern and easier to notice.

X=$(which ruby)

It also allows nesting, which is not possible with backticks.

e.g.

$($(EXPRESSION))

Uses

Store

Usually you’ll store the value.

$ X=$(which ruby)
$ echo "Ruby location: $X"
Ruby location: /usr/local/opt/ruby/bin/ruby
$ DATE=$(date)

Execute

The result can be executed without an in-between step.

$(which ruby)

That is will get result as /usr/local/opt/ruby/bin/ruby.

And then run it for you.

$ /usr/local/opt/ruby/bin/ruby

Which starts an interactive session.

Variation:

$(which ruby) -v
ruby 2.7.2p137 ...

I’ve seen this approach for setting environment variables before, where you call some CLI script like for AWS. It generates a few environment variables as a multi-line string, which is executed line by line in the current space.

Print

Use it with an echo command.

$ echo "Ruby location $(which ruby)"
Ruby location /usr/local/opt/ruby/bin/ruby