bug-bash
[Top][All Lists]
Advanced

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

Re: Bash style of if-then-else?


From: Michael Witten
Subject: Re: Bash style of if-then-else?
Date: Fri, 24 Sep 2010 11:59:23 -0700 (PDT)

On Fri, Sep 24, 2010 at 03:22, Marc Herbert <address@hidden> wrote:
>> On Thu, Sep 23, 2010 at 04:38:42PM -0500, Michael Witten wrote:
>>> This is also possible:
>>>
>>>   [ -f "$file" ] && do_something
>
> Note that this style is not compatible with set -e

I was going to note that same caveat in my original email, as I had recalled
running into that issue. However, my recollection seemed to be contradicted
by the manual:

  info bash 'Shell Builtin Commands' 'Modifying Shell Behavior' 'The Set 
Builtin'

which states:
  
  `-e'
       Exit immediately if a pipeline (*note Pipelines::), which may
       consist of a single simple command (*note Simple Commands::),
       a subshell command enclosed in parentheses (*note Command
       Grouping::), or one of the commands executed as part of a
       command list enclosed by braces (*note Command Grouping::)
       returns 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 in
       an `if' statement, 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 status is being inverted with `!'.  A trap on `ERR',
       if set, is executed before the shell exits.

       This option applies to the shell environment and each
       subshell environment separately (*note Command Execution
       Environment::), and may cause subshells to exit before
       executing all the commands in the subshell.

Specifically:

  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 in an
  `if' statement, 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 status is being
  inverted with `!'.

Even more specifically:

  The shell does not exit if the command that fails is
  ... part of any command executed in a `&&' or `||'
  list except the command following the final `&&' or
  `||'...

For instance, the following script:

  set -e
  false && echo 0
  echo 1

still echoes `1' when run with:

  GNU bash, version 4.1.7(2)-release (i686-pc-linux-gnu)

Sincerely,
Michael Witten



reply via email to

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