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

jason-tuhs--- via TUHS tuhs at tuhs.org
Wed Aug 5 06:35:35 AEST 2026


> Then why does
> echo me; echo $$
> ( echo parens ; echo $$ ; )
> { echo brace; echo $$ ; }
>
> Always echo the same pid?

This is a special case.

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

> $
>     Expands to the decimal process ID of the invoked shell. In a
>     subshell (see Shell Execution Environment ), '$' shall expand to the
>     same value as that of the current shell.

$ echo $$
16573

$ ( sleep 2 ; sleep 2 ; echo $$ ) & ps dx | egrep -B3 "sleep 2"
43470  0  Is    0:00.30 | `-- zsh
16573  0  S     0:00.01 |   `-- sh
31757  0  S     0:00.00 |     |-- sh
34834  0  SC    0:00.00 |     | `-- sleep 2
32128  0  R+    0:00.00 |     |-- ps dx
37095  0  S+    0:00.00 |     `-- egrep -B3 sleep 2

$ 16573

[1]   Done                    (sleep 2; sleep 2; echo ${$})


  -Jason



More information about the TUHS mailing list