bug-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] difference of $? and ${PIPESTATUS[0]}


From: Andreas Kähäri
Subject: Re: [Help-bash] difference of $? and ${PIPESTATUS[0]}
Date: Mon, 22 Apr 2024 09:39:50 +0200

On Mon, Apr 22, 2024 at 09:15:52AM +0200, felix wrote:
> Le Mon, Apr 22, 2024 at 07:44:48AM +0100, Kerin Millar a écrit :
> > On Mon, 22 Apr 2024, at 7:13 AM, felix wrote:
> > > ...
> > >   if ls /wrong/path | wc | cat - /wrong/path | sed 'w/wrong/path' 
> > > >/dev/null ; then
> > >       echo Don't print this'
> > >   fi ; echo ${?@Q} ${PIPESTATUS[@]@A}  $(( $? ${PIPESTATUS[@]/#/+} ))
> > >
> > >   ls: cannot access '/wrong/path': No such file or directory
> > >   cat: /wrong/path: No such file or directory
> > >   sed: couldn't open file /wrong/path: No such file or directory
> > >   '0' declare -a PIPESTATUS=([0]="2" [1]="0" [2]="1" [3]="4") 7
> > >
> > > Where $PIPESTATUS[0]=>2 and $?=>0 !!
> > > ...
> > > If so, "$?" have to be equivalent to "${PIPESTATUS[0]}", I think.
> > 
> > No. That would only be true in the event that the pipeline comprises a 
> > single command. The present documentation is correct.
> 
> I was wrong: the last is `${PIPESTATUS[-1]}' -> '${PIPESTATUS[3]}' in this 
> case,
> anyway
>          $PIPESTATUS[3]=>4 and $?=>0 !!
> 
> > It's worth reading the section of the manual that concerns "Pipelines".
> Reading this, if I could understand why
>   false;echo ${PIPESTATUS[0]} $?
>   1 1
> I'm still don't be able to explain this:
>   if false;then echo Don't print that's;fi; echo ${PIPESTATUS[0]} $?
>   1 0
> 
> -- 
>  Félix Hauri  -  <felix@f-hauri.ch>  -  http://www.f-hauri.ch

The exit status of an "if" statement is the exit status of the last
command executed within the body of the statement.

In your last example, nothing is executed by the body of the "if"
statement, so the exit status of the statement is zero.

>From the bash manual about this in the "Compound Commands" section:

        The exit status is the exit status of the last command
        executed, or zero if no condition tested true.

The PIPESTATUS array is set by virtue of executing the "false" command
in the "if"-satement's test.

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



reply via email to

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