Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14621

Re: Special parameter ?

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Special parameter ?
Date Fri, 21 Sep 2018 08:57:05 -0400
Lines 53
Approved bug-bash@gnu.org
Message-ID <mailman.1083.1537534660.1284.bug-bash@gnu.org> (permalink)
References <201809211123.w8LBNvHe018414@omac.gsyc.urjc.es>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace usenet.stanford.edu 1537534660 4969 208.118.235.17 (21 Sep 2018 12:57:40 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org, bash@packages.debian.org
To esoriano@gsyc.urjc.es
Envelope-to bug-bash@gnu.org
Mail-Followup-To esoriano@gsyc.urjc.es, bug-bash@gnu.org, bash@packages.debian.org
Content-Disposition inline
In-Reply-To <201809211123.w8LBNvHe018414@omac.gsyc.urjc.es>
User-Agent NeoMutt/20170113 (1.7.2)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From 139.137.100.1
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14621

Show key headers only | View raw


On Fri, Sep 21, 2018 at 01:23:57PM +0200, esoriano@gsyc.urjc.es wrote:
> The man page of bash (Special Parameters section) says:
> 
>    ? expands to the exit status of the most 
>    recently executed foreground pipeline.
> 
> Nevertheless, background commands also modify the
> value of this variable. 

This is incorrect.  The exit status of a background command can only
be retrieved by calling "wait" (or if the background command itself
chooses to write the forthcoming exit status to stdout, or to a file,
etc.).

> Example:
> 
> esoriano@omac:~$ false
> esoriano@omac:~$ sleep 2 &
>        (wait 3 seconds)
> esoriano@omac:~$ echo $?
> 0
> esoriano@omac:~$  
> 
> In this example, the most recently executed foreground 
> command is false, so the value of $? should be 1.

Under "Lists":

       If a command is terminated by the control operator &,  the  shell  exe‐
       cutes  the command in the background in a subshell.  The shell does not
       wait for the command to finish, and the return status is  0.

You're seeing the status from the creation of the background job (which is
always 0), not from its completion.

Compare:

wooledg:~$ (sleep 3; exit 47) & pid=$!
[1] 19112
wooledg:~$ echo $?
0
wooledg:~$ 
[1]+  Exit 47                 ( sleep 3; exit 47 )
wooledg:~$ echo $?
0
wooledg:~$ wait $pid; echo $?
47

Even after the background job completes, its exit status is never
propagated into the foreground shell's $? parameter.  The only way
to retrieve the status is with a wait command which is given a PID or
a job specifier.

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Special parameter ? Greg Wooledge <wooledg@eeg.ccf.org> - 2018-09-21 08:57 -0400

csiph-web