On Thursday, April 11, 2013 04:15:45 PM Lenga, Yair wrote:
> ( echo "$!") &
> ( echo "$!") &
>
> According to POSIX, both calls should print "" (nothing). As the background
> command does not come from the "current" shell.
>
> With BASH4, The first call will print "" (nothing), second call will print
> the PID of the first call.
>
> This is very confusing, and probably conflict with POSIX standard which
> indicate that "$!" apply to "CURRENT" shell.
The backgrounding of each list member is sequential. Presumably $! should be
set immediately after each fork and inherited by subshells like most other
variables. See "Command Execution Environment" in the manual or POSIX.
`x &', `{ x; } &' and, `(x) &' -- where `x' is a simple command are all
identical. The subshell group () is redundant in your example.
Note the wait doesn't impact these results. It just forces the output to be
ordered.
$!/usr/bin/env bash
for sh in {{b,d}a,z,{m,}k,po}sh bb sh; do
printf %-6s "${sh}:"
"$sh" /dev/fd/0
echo
done <<\EOF
${ZSH_VERSION+false} || emulate sh
n=5
while [ $((n -= 1)) -ge 0 ]; do
printf "%s " "${!:-empty}" & wait
done
EOF
bash: empty 2787 2788 2789 2790
dash: empty 2793 2794 2795 2796
zsh: 0 2800 2801 2802 2803
mksh: empty 2807 2809 2810 2811
ksh: empty 2814 2815 2816 2817
posh: empty 2820 2821 2822 2823
bb: empty 2826 2827 2828 2829 # busybox
sh: empty 2832 2833 2834 2835 # bash POSIX mode
--
Dan Douglas