71

I'm studying shell scripting with bash and I need to know the difference between (...) and {...}. How does one select between the two when writing a script?

85
+100

If you want the side-effects of the command list to affect your current shell, use {...}
If you want to discard any side-effects, use (...)

For example, I might use a subshell if I:

  • want to alter $IFS for a few commands, but I don't want to alter $IFS globally for the current shell
  • cd somewhere, but I don't want to change the $PWD for the current shell

It's worthwhile to note that parentheses can be used in a function definition:

  • normal usage: braces: function body executes in current shell; side-effects remain after function completes

    $ count_tmp() { cd /tmp; files=(*); echo "${#files[@]}"; }
    $ pwd; count_tmp; pwd
    /home/jackman
    11
    /tmp
    $ echo "${#files[@]}"
    11    
    
  • unusual usage: parentheses: function body executes in a subshell; side-effects disappear when subshell exits

    $ cd ; unset files
    $ count_tmp() (cd /tmp; files=(*); echo "${#files[@]}")
    $ pwd; count_tmp; pwd
    /home/jackman
    11
    /home/jackman
    $ echo "${#files[@]}"
    0
    

Documentation

  • 11
    After many years of shell development I didn't know you could use parentheses to run functions in subshells. What a great idea to avoid polluting the global namespace! – l0b0 Apr 8 '15 at 7:33
  • 7
    Using the local keyword goes a long way to cleaning up that pollution. – glenn jackman Apr 8 '15 at 10:54
  • 2
    Yeah, but you have to remember to declare every variable local, and it clutters the code. – l0b0 Apr 8 '15 at 15:00
  • 4
    Hint: If you want side-effect-free functions but avoid the unusual function declaration syntax (which code editors may not be aware of) then just use parentheses on the function call rather than the declaration: pwd; (count_tmp); pwd; – Juve Jul 8 '15 at 15:11
  • 2
    to the shell... foo() (:;) is equivalent to foo() { (:;); } That is how it reports it if you ask! – anthony Nov 15 '16 at 6:01
23

From the official bash documentation:

()

( list )

Placing a list of commands between parentheses causes a subshell environment to be created, and each of the commands in list to be executed in that subshell. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

{}

{ list; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

9

Code in '{}' is executed in the current thread/process/environment and changes are preserved, to put it more succinctly, the code is run in the current scope.
Code in '()' is run inside a separate, child process of bash that is discarded after execution. This child process is often referred to as a sub-shell and can be thought of as a new, child-like scope.

As an example consider the following...

 ~ # { test_var=test }
 ~ # echo $test_var
 test
 ~ # ( test_var2=test2 )
 ~ # echo $test_var2

 ~ # 

Notice in the first example with '{}' the variable is still set even after the closing '}', whereas in the example with '()' the variable is not set outside the scope of the '()'.

4

(...) are used to run code in a sub-shell. Code used bewteen {...} won't be used in a sub-shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.