In my Bash shell the $$
variable expands to the process ID of the shell.
So why does ps
print its own name when I execute this?
$ bash -c 'ps -p $$ -o comm='
ps
When I follow it with a no-op command, ps
does print the name of the shell.
$ bash -c 'ps -p $$ -o comm= && true'
bash
Versions:
- GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
- ps from procps-ng 3.3.16
ps
exits so it can then runtrue
if needed. It wouldn't need to keep itself alive untiltrue
exits, though, but Bash only optimizes the case of a single command, not any tail-end command. However, ksh93 optimizes that too, e.g.ksh -c 'ps -p $$ -o comm=; ps -p $$ -o comm='
printsksh
,ps
– ilkkachu Aug 31 '23 at 17:11