Jay Taylor's notes

back to listing index

shell - Difference between parentheses and braces in terminal? - Unix & Linux Stack Exchange

[web search]
Original source (unix.stackexchange.com)
Tags: bash shell-scripting curly-braces parenthesis nuances unix.stackexchange.com
Clipped on: 2020-12-14

Asked 4 years, 9 months ago
Viewed 6k times
20
This question already has answers here:
Closed 4 years ago.
( du /etc; du /var; ) > tmp.txt

{ du /etc; du /var; } > tmp.txt

Is there a difference between the () and {}?

The output of tmp.txt seems exactly the same, and I was wondering whether i'm missing something here.

share | edit | | flag |
Image (Asset 3/5) alt=
See Grouping Commands under 2.9.4 Compound Commands – don_crissti Mar 4 '16 at 17:10
  • 2
    try inserting exit ; between the du. – Archemar Mar 4 '16 at 17:10
  • This is the closest answer I found in the web. The exact issue has not been explained in this site. – user79743 Mar 4 '16 at 18:30
  • 1
    Both of them are Grouping Commands, but { list; } executes commands in current shell environment. – user79743 Mar 4 '16 at 18:31
  • 27

    Parentheses cause the commands to be run in a subshell.

    Braces cause the commands to be grouped together but not in a subshell.

    Given that your example does not use side-effects, there is no real difference between both. If there were side-effects, e.g. setting or modifying shell variables, there is a difference as such side-effects applied to a sub-shell will be forgotten when this sub-shell ends.

    If you however take a closer look and compare the behavior of different shell implementations, it becomes confusing:

    The Bourne Shell e.g. runs grouped commands in a subshell in case there is an I/O redirection and ksh93 avoids subshells by implementing virtual subshell behavior that is done by creating a temporary copy of new parameters. Whether this is always 100% correct is not known, ksh93 Version M 1993-12-28 s+ from 2009 e.g. implements $(...) incorrectly and $(alias a=b) affects the main shell.

    So in general: if you are interested in specific aspects, be careful and check your shell for it's actual behavior.

    share | edit | | flag |
    answered Mar 4 '16 at 17:07
    Image (Asset 4/5) alt=
    (I wouldn't have bothered mentioning it but since you're maintaining a Bourne shell ;)), ...not in a subshell, except for the Bourne shell that runs compound commands in subshells when redirected. – Stéphane Chazelas Mar 4 '16 at 17:11
  • Well parentheses grant the commands to be in a subshell, with braces, it still may happen ;-) but ksh93 does not even create a real subshell for parentheses, but rather emulates the effects of a subshell by using a temporary new copy of parameters. – schily Mar 4 '16 at 17:14
  • Yes, you'll notice that the POSIX spec is careful to talk only of subshell environment with no implication that it may involve a child process. And with many shells that implement subshells using a child process, (a;b) will not spawn more processes than {a;b;} (if b is not a builtin nor function nor compound command and there's no local trap) as b will be executed in that child subshell process. – Stéphane Chazelas Mar 4 '16 at 17:20
  • So if I understand correctly, the only difference is that: parentheses use a subshell and braces don't? And the output stays the same, regardless of the subshell (in this case). Thankyou, this helped! – Nelske Mar 4 '16 at 17:32
  • See my new paragraph about side-effects. In your example, there is no visible difference. – schily Mar 4 '16 at 17:36
  • Not the answer you're looking for? Browse other questions tagged or ask your own question.

    Hot Network Questions

    more hot questions

    site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.12.14.38169

    Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.

    This site is not affiliated with Linus Torvalds or The Open Group in any way.