[Top][All Lists]

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

Re: exit-on-error (set -e) option and command after "if" not found

From: Eric Blake
Subject: Re: exit-on-error (set -e) option and command after "if" not found
Date: Wed, 22 Jun 2011 17:11:31 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.10

On 06/22/2011 06:51 AM, Dmitry Bolshakov wrote:
> Hi
> set -e
> if nosuchcommand | grep blabla
> then
>   echo ERROR: blabla was found
>   exit 1
> fi
> echo it was unexpected for me, I thougt this line should not be echoed

Not a bug; this last echo is supposed to happen.

The documentation for 'set -e' is clear:

 Exit immediately if a pipeline (which may consist  of  a
 single  simple command),  a subshell command enclosed in
 parentheses, or one of the commands executed as part  of
 a  command  list  enclosed  by braces (see SHELL GRAMMAR
 above) exits with a non-zero status.  The shell does not
 exit  if  the  command that fails is part of the command
 list immediately following a  while  or  until  keyword,
 part  of  the  test  following  the  if or elif reserved
 words, part of any command executed in a && or  ⎪⎪  list
 except  the  command  following  the final && or ⎪⎪, any
 command in a pipeline but the last, or if the  command's
 return  value  is being inverted with !.

Since the pipeline 'nosuchcommand | grep blabla' is executed as part of
the test following an 'if', then 'set -e' does not abort the shell on

'set -e' is a bear to use - it generally does not protect you from
everything that you think it ought to, and has a number of portability
bugs to boot as you migrate between versions of bash or between other
shells.  Not to mention that the POSIX folks can't even seem to get it
right; the definition of 'set -e' had to be amended even after POSIX
2008 to match historical practice, which in turn disagreed with bash
practice at the time.

Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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