In bash, both ()
and $()
create a subshell.
What\'s the difference between each other? What\'s their typical usages?
()
just creates a compound command, running the commands inside the parentheses. $()
does the same, but also substitutes the output.
From the docs:
(list)
list
is executed in a subshell environment ... Variable assignments and builtin
commands that affect the shell's environment do not remain in effect after the command completes. The return status is
the exit status of list
.
Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:
$(command)
or
`command`
Bash performs the expansion by executing command
and replacing the command substitution with the standard output of the command,
with any trailing newlines deleted.