help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] questions about errexit, pipes to while loops


From: Chet Ramey
Subject: Re: [Help-bash] questions about errexit, pipes to while loops
Date: Tue, 27 Mar 2012 12:59:58 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2

On 3/25/12 4:40 PM, Jonathan Baccash wrote:
> As far as I can tell, the documentation doesn’t match the behavior for bash
> (using bash 3.1.17) in the following scripts. First, this script works as
> expected:
> 
>  
> 
> set -e
> cat /dev/zero | false
> echo blah $?
> 
>  
> 
> The script exits with status 1 and does not print blah 1. 

While that might be what you expected, it was a bug and did not match the
documentation or Posix (at the time), which specified that set -e affected
only simple commands.  A pipeline, even one that ends with a simple
command, is not a simple command.

That was fixed in 2008, between bash-3.2 and bash-4.0.


> This one does not
> appear to work in accordance with docs:
> 
>  
> 
> set -e
> 
> cat /dev/zero | while true; do false; done
> 
> echo blah $?

This one works correctly (for the time; Posix and bash have changed since).
Not only is a pipeline not a simple command, a while loop is a compound
command.

>  
> 
> Another weird thing I noticed is that this:
>  
> set -e
> cat </file/that/doesnt/exist
> 
> exits with a non-zero exit status, 

Again, a failing simple command.  This time, it happens to be a redirection
failure.

> 
>  
> 
> set -e
> while true; do false; done </file/that/doesnt/exist 
> 
> does not. Shouldn’t it?

No, not according to what bash and Posix specified at the time (and still
do, actually).

As I alluded to above, bash and Posix have changed since 2008.  Bash-4.2
implements the current Posix specification, which exits the shell if any
command fails -- not just simple commands -- with the usual exceptions
listed in the man page.  Interestingly, no shell exits on your last
example above.

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]