bug-bash
[Top][All Lists]
Advanced

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

Re: Exit status of "if" statement?


From: DJ Mills
Subject: Re: Exit status of "if" statement?
Date: Mon, 9 Apr 2012 14:14:08 -0400

On Mon, Apr 9, 2012 at 1:40 PM, Pierre Gaston <pierre.gaston@gmail.com> wrote:
> On Mon, Apr 9, 2012 at 8:31 PM, Dan Stromberg <drsalists@gmail.com> wrote:
>>
>> What should be the behavior of the following?
>>
>> if cmd1
>> then
>>    cmd2
>> fi && if cmd3
>> then
>>   cmd4
>> fi
>>
>> I've not joined two if's with a short-circuit boolean before, but I'm
>> suddenly working on a script where someone else has.
>>
>> Playing around, it appears that cmd1 and cmd3 have no direct impact on
>> the exit codes of the two if's, while cmd2 and cmd4 do (if cmd1 or
>> cmd3 evaluate true).  Is this the defined behavior in POSIX shell?  In
>> bash?  In bash symlinked to /bin/sh?  In dash?
>>
>> TIA!
>>
> posix says "The exit status of the if command shall be the exit status
> of the then or else compound-list that was executed, or zero, if none
> was executed."
>
> The bash documentation says essentialy the same.
>

>From ''help if'':
The exit status of the entire construct is the exit status of the last
command executed, or zero if no condition tested true.

It wouldn't make sense if the conditions affected the exit status. The
whole purpose of 'if' is to check the exit status of a command. If
that status persisted, things like set -e and the ERR trap would still
trigger. That would be a nightmare.



reply via email to

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