1. Overview

In this tutorial, we’ll discuss the differences between ${} and $() in Bash. First, we’ll briefly review how command substitution and parameter expansion work in Bash. Then, we’ll dive into the differences between the two and discuss some use cases.

2. Command Substitution

Command substitution in Bash allows us to execute a command and substitute it with its standard output. Note this command executes within a subshell, which means it has its own environment and so it will not affect the parent shell’s environment.

Let’s now check a way to achieve command substitution:

$ var_date=$(date) && echo $var_date
Sat May  1 11:33:36 IST 2021

In this case, we are using the $(..) form where the command to be executed is enclosed between the parentheses. This form is the recommended syntax of command substitution.

3. Brace Parameter Expansion

Let’s run a couple of examples to understand how parameter expansion works:

$ price=5
$ echo "$priceUSD"

$ echo "${price}USD"

As we can observe from the output, we use {..} here as a disambiguation mechanism for delimiting tokens.

Adding double quotes around a variable tells Bash to treat it as a single word. Hence, the first echo statement returns an empty output since the variable priceUSD is not defined.

However, using ${price} we were able to resolve the ambiguity. Clearly, in the second echo statement, we could concatenate the string USD  with the value of the price variable that was what we wanted to do.

Another interesting use of brace expansion is to generate arbitrary strings:

$ mkdir /home/shubh/baeldung/{linux,cs,java}
$ ls -lrt /home/shubh/baeldung/
total 0
drwxr-xr-x 1 shubh shubh 512 Mar 28 15:24 linux
drwxr-xr-x 1 shubh shubh 512 Mar 28 15:24 cs
drwxr-xr-x 1 shubh shubh 512 Mar 28 15:24 java

Note how we used the brace expansion to easily create multiple directories at once.

4. Differences

As we’ve seen in the previous sections, we should use $() when we want to capture the command result written to stdout. In such a case, the command, which will be executed in a new sub-shell, has to be specified within the parenthesis.

Using brace parameter expansion, on the other hand, we can specify a variable within {} to protect it against expansion. This is useful when the characters immediately following it can be interpreted as part of the variable name.

Let’s now verify those concepts using an example:

$ due_date="01-01"
$ echo "Status as of $(date +%m-%d-%Y) : The delivery is due on ${due_date}-2022"
Status as of 05-01-2021 : The delivery is due on 01-01-2022

In this example, we’re using the ${} form, i.e., brace expansion, to print the value of the due_date variable. Also, we are using the command substitution $()  to execute the date command print the current date.

5. Conclusion

In this article, we’ve learned the difference between the use of ${} and $() in Bash. First, we briefly reviewed the concepts of command substitution and parameter expansion. Finally, we discussed the differences along with some use-cases for the ${} and $() syntax.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!