Jay Taylor's notes
back to listing indexshell - Difference between parentheses and braces in terminal? - Unix & Linux Stack Exchange
[web search]Stack Exchange Network
Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange( 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.
{ list; }
executes commands in current shell environment.
– user79743
Mar 4 '16 at 18:31
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.
(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
Not the answer you're looking for? Browse other questions tagged shell or ask your own question.
-
-
-
-
7
Linked
Related
Hot Network Questions
-
Can I delete a field in awk?
-
Why we need DFT already we have DTFT?
-
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy
-
Calculating the percentage of (+)-2- butanol in the sample
-
How could a 6-way, zero-G, space constrained, 3D, flying car intersection work?
-
Effects of being hit by an object going at FTL speeds
-
Is a password-protected stolen laptop safe?
-
How to say there is no water available?
-
What is the critical coupling constant in an Ising model and how to spot it?
-
Retrograde analysis problem
-
Safely preserving a manuscript for 700 years
-
I'm a piece of cake. Anti-me can be fatal
-
Is technology a natural consequence of civilization?
-
Are cadavers normally embalmed with "butt plugs" before burial?
-
How did Alice return from Wonderland the very first time?
-
Why is acceleration directed inward when an object rotates in a circle?
-
Given a legal chess position, is there an algorithm that gets a series of moves that lead to it?
-
Risks of using home equity for high risk market investing
-
Are metals and other elements in every continent?
-
Why is it easier to handle a cup upside down on the finger tip?
-
Suppose you put your hands on a wall and push it
-
How to \futurelet the token after a space
-
Why it is important to write a function as sum of even and odd functions?
-
Can I fly a STAR if I can't maintain the minimum speed for it?
This site is not affiliated with Linus Torvalds or The Open Group in any way.