Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash subshell: parentheses:() VS dollar-parentheses:$()

Tags:

In bash, both () and $() create a subshell.

What's the difference between each other? What's their typical usages?

like image 952
Bohr Avatar asked Oct 19 '13 04:10

Bohr


1 Answers

() just creates a compound command, running the commands inside the parentheses. $() does the same, but also substitutes the output.

From the docs:

  1. (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.

  2. 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.

like image 89
Carl Norum Avatar answered Sep 24 '22 02:09

Carl Norum