Skip to main content
Added a small example to explain the meaning behind "side effect".
Source Link

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.

To understand the "side-effect", see the following examples:

Using parentheses ():

v="test"; 
( echo $v; v="modified"; echo $v; ); 
echo $v;

# output:
#   test
#   modified
#   test

Using curly braces {}:

v="test"; 
{ echo $v; v="modified"; echo $v; }; 
echo $v;

# output:
#   test
#   modified
#   modified

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.

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.

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.

To understand the "side-effect", see the following examples:

Using parentheses ():

v="test"; 
( echo $v; v="modified"; echo $v; ); 
echo $v;

# output:
#   test
#   modified
#   test

Using curly braces {}:

v="test"; 
{ echo $v; v="modified"; echo $v; }; 
echo $v;

# output:
#   test
#   modified
#   modified

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.

side-effects added
Source Link
schily
  • 19.8k
  • 5
  • 41
  • 61

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.

Parentheses cause the commands to be run in a subshell.

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

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.

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.

added 634 characters in body
Source Link
schily
  • 19.8k
  • 5
  • 41
  • 61

Parentheses cause the commands to be run in a subshell.

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

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.

Parentheses cause the commands to be run in a subshell.

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

Parentheses cause the commands to be run in a subshell.

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

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.

Source Link
schily
  • 19.8k
  • 5
  • 41
  • 61
Loading