wiki.bash-hackers.org/syntax/ccmd/grouping_subshell.md

32 lines
849 B
Markdown
Raw Normal View History

2023-04-15 13:23:49 +02:00
====== Grouping commands in a subshell ======
===== Synopsis =====
2023-04-15 13:53:05 +02:00
<code>
( <LIST> )
</code>
2023-04-15 13:23:49 +02:00
===== Description =====
2023-04-15 13:53:05 +02:00
The [[syntax:basicgrammar#lists|list]] ''<LIST>'' is executed in a separate shell - a subprocess. No changes to the environment (variables etc...) are reflected in the &quot;main shell&quot;.
2023-04-15 13:23:49 +02:00
===== Examples =====
Execute a command in a different directory.
2023-04-15 13:53:05 +02:00
<code bash>
2023-04-15 13:23:49 +02:00
echo &quot;$PWD&quot;
( cd /usr; echo &quot;$PWD&quot; )
echo &quot;$PWD&quot; # Still in the original directory.
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
===== Portability considerations =====
* The subshell compound command is specified by POSIX.
* Avoid ambiguous syntax.
2023-04-15 13:53:05 +02:00
<code bash>
2023-04-15 13:23:49 +02:00
(((1+1))) # Equivalent to: (( (1+1) ))
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
===== See also =====
* [[syntax:ccmd:grouping_plain | grouping commands]]
* [[http://mywiki.wooledge.org/SubShell | Subshells on Greycat's wiki]]