What’s the Difference Between ${} and $() in Bash

GNU-based operating systems like Linux rely on a command language interpreter or shell called Bash to accomplish most of their computing tasks and objectives.

The author behind Bash (Stephen Bourne) is also principally referenced in its acronym (Bourne-Again SHell). In terms of compatibility, Bash and sh are a perfect match. Useful features evident in the C shell (csh) and the Korn shell (ksh) are also incorporated in this shell language interpreter.

In terms of implementation, Bash is intended to conform to IEEE POSIX specification (IEEE Standard 1003.1) Tools portion and IEEE POSIX Shell. Therefore, as an interactive and programming tool, the functional improvements offered by Bash outshine the ones present in sh.

Linux and other GNU-based operating systems’ users are not forced to solely rely on Bash are their primary command language interpreter since shell versions like csh are also pre-packaged in these systems. Bash is only configured to function as a default Linux shell due to its unique shell attributes like portability.

Comparing $() versus ${} in Bash

To compare these two attributes associated with the Bash environment, we will have to reiterate $() to $(command) and ${} to ${parameter}. We can therefore attempt to define these two Bash entities.

Understanding $(command)

$(command) in Bash relates to command substitution. To be more specific, the command portion in $(command) is executed, its output captured and printed on the standard output.

The syntax behind $(command) is token-level. Consider the execution of the following command which intends to print out the current date on the terminal.

$ echo "Today is $(date). A fine day."
Print Date in Linux
Print Date in Linux

The above execution can be explained in the following manner. Firstly, the date command is executed and its output is included as an argument to the echo command.

Here, we could presume that the command substitution is executed in a separate sub-shell before its re-inclusion on the main shell.

Understanding ${parameter}

${parameter} in Bash relates to parameter substitution. As per the Bash manual page, a parameter is a value-storing entity. The parameter entity can be associated with a name, a number, or special characters.

The braces {} imply that we a dealing with a positional parameter or that the parameter in use (parameter value) is succeeded by a non-interpretive character that is not to be associated with the parameter name e.g ${animal}s.

As for positional parameters, one or more digits can be used to denote it (with the exception of the single digit 0). Therefore, if we have a variable named $variable_name, the parameter substitution ${parameter} can be used to retrieve the associated variable value.

Consider the following variable declaration command:

$ animal=lion

Let us call the animal variable in an attempt to pluralize the variable value:

$ echo $animals   
Declare Variable in Command
Declare Variable in Command

Nothing is printed on the standard output because Bash does not understand the variable $animals. However, we can successfully call our $animals variable using the parameter substitution approach in the following manner:

$ echo ${animal}s
Call Variable in Command
Call Variable in Command

In some scenarios, an exclamation point might succeed the first left curly brace of parameter substitution implementation. In this case, the concept of variable indirection takes place.

The variable value takes precedence over the parameter value as demonstrated below:

$ animal=lion 
$ echo $animal
$ lion=rafiki
$ echo $lion
$ echo ${!animal}
Precedence Over Parameter Value
Precedence Over Parameter Value

Hope this article has shed some light on the differences between $() and ${} in Bash. Feel free to leave a comment or feedback.

1 thought on “What’s the Difference Between ${} and $() in Bash”

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.