[TUHS] Use of "{}" in the Shell?

jason-tuhs--- via TUHS tuhs at tuhs.org
Wed Aug 5 08:40:42 AEST 2026


>  - Purpose of a subshell is to avoid any changes in its environment to kick 
> back into the current shell
>  - ... while keeping non-exported variables available.  They wouldn't be 
> inherited by a new sh command.

> I wonder if keeping even $$ was an unintentional side effect. Or even 
> intentional:
> The subshell shall see and behave very much like the original shell, just not 
> leave any traces in the environment after exiting?

Correct.  https://pubs.opengroup.org/onlinepubs/009696599/utilities/xcu_chap02.html#tag_02_12

> A subshell environment shall be created as a duplicate of the shell 
> environment, except that signal traps set by that shell environment 
> shall be set to the default values. Changes made to the subshell 
> environment shall not affect the shell environment.

"Environment" in this case having a broad meaning including shell 
functions, variables, open FDs, CWD, umask, etc.


Forking a new process for a subshell is not a requirement, it is merely an 
implementation detail, and it is not universal.

E.g., ksh93 does not fork for (some) subshells (presumably as a 
performance optimization?), while pdksh, bash, zsh, and ash, do.

 	/home/jason-241229: ksh93
 	$ echo $$ ; ( /bin/sleep 1 ; /bin/sleep 1 ; echo $$ )
 	22589
 	22589

 	/home/jason-241231: while true ; do ps dx | egrep -B3 "sleep 1" ; done
 	52328  -  S     0:00.15 |-- xterm -fn Source Code Pro Medium 14
 	65138 25  Is    0:00.39 | `-- zsh
 	22589 25  S     0:00.01 |   `-- ksh93
 	29282 25  SC+   0:00.00 |     `-- /bin/sleep 1

 	hashbrown/home/jason-241230: ksh
 	$ echo $$ ; ( /bin/sleep 1 ; /bin/sleep 1 ; echo $$ )
 	82173
 	82173

 	/home/jason-241232: while true ; do ps dx | egrep -B3 "sleep 1" ; done
 	65138 25  Is    0:00.41 | `-- zsh
 	82173 25  S     0:00.00 |   `-- ksh
 	80219 25  S+    0:00.00 |     `-- ksh
 	80250 25  SC+   0:00.00 |       `-- /bin/sleep 1

 	/home/jason-241230: pkg which =ksh93 =ksh
 	/usr/local/bin/ksh93 was installed by package ksh93-93.u_4,2
 	/usr/local/bin/ksh was installed by package pdksh-5.2.14p2_7


  -Jason


More information about the TUHS mailing list