Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use set -e to stop bash script on first error.

All work OK unless I use command with &&:

$ cat script
set -e
cd not_existing_dir && echo 123
echo "I'm running! =P"
$
$ ./script
./script: line 2: cd: not_existing_dir: No such file or directory
I'm running! =P
$

compared with:

$ cat script
set -e
cd not_existing_dir
echo "I'm running! =P"
$
$ ./script
./script: line 2: cd: not_existing_dir: No such file or directory
$

The first example still echoes I'm running!, but the second one doesn't. Why do they behave differently?

UPD. Similar question: http://stackoverflow.com/questions/6930295/set-e-and-short-tests

share|improve this question
    
What do you expect to happen in the first example? – Flup yesterday
    
@Flup I expect script to stop after unsuccessful cd command – 907th yesterday
    
See BashFAQ #105 for a general discussion of places where set -e behavior is surprising. – Charles Duffy yesterday
up vote 9 down vote accepted

The set -e option doesn't have effect in some situations, and this is the standard behavior and portable across POSIX compliant shell.


The failed command is part of pipeline:

false | true; echo printed

will print printed.

And only the failure of the pipeline itself is considered:

true | false; echo 'not printed'

will print nothing.


The failed command run in the compound list following the while, until, if, elif reserved word, a pipeline beginning with the ! reserved word, or any command as part of && or || list except the last one:

false || true; echo printed

The last command fails still make set -e affected:

true && false; echo 'not printed'

The subshell fails in a compound command:

(false; echo 'not printed') | cat -; echo printed
share|improve this answer
    
Thank you! It would be clearer to use echo "printed" and echo "not_printed" in your examples (instead of echo 1). – 907th yesterday
2  
Note that set -e causes an exit in (false && true); echo not here, but not in { false && true; }; echo here, though YMMV with different shells and even different versions of a same shell. I wouldn't touch set -e with a barge pole and would do proper error handling instead. – Stéphane Chazelas yesterday

my guess is if-then condition as a whole evaluate to true.

I tried

set -e
if cd not_existing_dir
then  echo 123
fi
echo "I'm running! =P"

who give

-bash: cd: not_existing_dir: No such file or directory
I'm running! =P

error code is catch by if condition, thus bash will not trigger end of execution.

share|improve this answer
    
But I do not use if ... fi – 907th yesterday
    
I know, this is an implicit if. – Archemar yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.