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

前端 未结 1 921
慢半拍i
慢半拍i 2021-02-02 11:56

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

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

相关标签:
1条回答
  • 2021-02-02 12:37

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

    0 讨论(0)
提交回复
热议问题