When using -e in bash, exclamation mark before passing the command still does not throw script errors

I thought the following script would just print 'hello' and then exit with '1'

#!/bin/bash -e

! echo hello
echo world

      

However, it prints

hello
world

      

and exits from 0

the following script exits with 1:

#!/bin/bash -e

! echo hello

      

and also the following

#!/bin/bash -e

echo hello | grep world
! echo hello
echo world

      

but for some reason the -e option fails to crash the script when the command returns an unsuccessful exit code due to! on the halfway. Can anyone suggest an explanation that will make me feel better?

+3


source to share


1 answer


http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin

The -e command will crash the script for any command that returns non-zero code, except in some cases, such as loops or commands that return 0, but inverted with! option. So yours ! echo hello

will return 1 (0, inverted with !

), but the parameter -e

won't work.



But if you do exit status 42

, you will have a crashing script.

+2


source







All Articles