bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Possible bug with BASH V4: The "$!"


From: Lenga, Yair
Subject: RE: Possible bug with BASH V4: The "$!"
Date: Fri, 12 Apr 2013 11:28:00 +0000

Chet,

Thanks for the detailed feedback. I double check the documentation (man, 
changelog)  issues, those seems to be local problems with our system (for some 
strange reason, the man pages were mirrored from RH4 image).

One last comment about the "double subshell" case:
> ( echo "$!") &
> ( echo "$!") &

The detail of the "$!" include reference to "current subshell", and seems to 
conflict with the "Shell Execution Environment", I'm not sure how to resolve 
those conflicts.

However, the implemented behavior can lead to strange bugs, as it is very 
unexpected. My 2 cents is that the Bash3 behavior or reseting the "$!" in sub 
shells is more likely to prevent errors, and will still be in compliance with 
the POSIX text. In the following (not very practical) example:

( DO_somthing ; if [ PROBLEM ] ; then kill $!; fi ; sleep 20) &
...
( DO_somthing ; if [ PROBLEM ] ; then kill $!; fi ; sleep 20) & 

The user is very unlikely to expect the second subshell to kill the first 
subshell.

In any case, I've modified my scripts to use "${BASHPID:-$!}" to get them to 
work in BASH4 and BASH3. Again, Many thanks.

Yair.


-----Original Message-----
From: Chet Ramey [mailto:address@hidden 
Sent: Thursday, April 11, 2013 2:03 PM
To: Lenga, Yair [ICG-MKTS]
Cc: 'address@hidden'; 'Dan Douglas'; 'address@hidden'
Subject: Re: Possible bug with BASH V4: The "$!"

On 4/11/13 12:15 PM, Lenga, Yair wrote:
> Dan, Chet: Many thanks for the info about BASHPID.
> 
>  I've checked BASHPID on RH6, and it looks OK. Few comments related to making 
> the change visible, and POSIX compliance.
> 
> + The 'set' command, does not print the BASHPID, this make it very hard to 
> find it (unless you read every line in the BASH info file !).
>    It will be very helpful to include  BASHPID there (if set).

It does, but BASHPID is one of the special shell variables that springs into 
existence only when it's referenced.  Run the following script; it should 
display two lines of output:

sleep 1&
echo $BASHPID
set | grep ^BASHP

> + The man page list BASH_VERSION, etc., but no indication of BASHPID. It's 
> probably a good idea to put a note next to '$!' about BASHPID.

It's in there, and has been since bash-4.0.

> + Could not find any change log on my system (probably a problem with RH). 
> Not sure if it's documented.

That depends on your distribution.  There is a summary changelog (CHANGES), a 
file listing new features (NEWS), and a detailed change log
(CWRU/changelog) in the FSF bash distribution.

> Also, I have a question about POSIX mode (and POSIX compliance).
> The POSIX says  '$!' is "most recent background command executed from the 
> current shell".
> 
> So in :
> 
> ( 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.

The bash behavior is correct.  The Posix requirement that (...) subshells 
create a `subshell environment', which is an exact copy of the parent shell's 
state with a couple of minor exceptions, trumps the text you quoted.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12

for the exact reference.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]