Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving $! (process ID of last background command) as an array element

I can't understand the way zsh 5.9 behaves when assigning $! to an array:

unset a b c

sleep 3 &

a=$!
b[1]=$!
c[1]=${!}

typeset -p a b c

output:

typeset a=21391
typeset -a b=( '$!' )
typeset -a c=( 21391 )

Getting b=( '$!' ) doesn't feel right... Is it a feature? A regression?

like image 781
Fravadona Avatar asked Aug 07 '26 04:08

Fravadona


1 Answers

The problem's been fixed in commit 5977d3c


But IMHO it's best not to rely on arr[1]=$! anymore and use the following workaround to be portable across zsh versions:

arr[1]=${!}
like image 161
Fravadona Avatar answered Aug 08 '26 23:08

Fravadona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!