Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

When Bash -e Doesn't Exit as You Expect

DZone's Guide to

When Bash -e Doesn't Exit as You Expect

Denny Zhang provides his simple test that explains how to deal with any unexpected or unhandled errors that involve bash -e.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

In bash scripting, it's a good practice to exit for any unexpected or unhandled errors. Usually, I enforce this by 'bash -e my_script.sh'. Today I got a surprise with 'bash -e'.

Check out the simple test below, because you might get bitten by this as well.

bash_exit.png

Source: http://dennyzhang.com/bash_errcode_exit

Here is my previous assumption: suppose we run a shell code block with 'bash -e' or 'set -e'. If any commands have problems in the middle, the whole code block shall fail and quit.

The Usual Case of 'bash -e'

As we expect in below test, "ls /wontexists" fails. Thus, we don't see the further output generated by the second echo command.

cat > /tmp/test1.sh << EOF
#!/bin/bash
echo "msg1" && ls /wontexists
echo "should not see this"
EOF

bash -xe /tmp/test1.sh
echo $?

Problematic Example: 'bash -e' Doesn't Exit

Running the below code, we will see an output of "should not see this". And $? is zero! Strange, isn't it?

cat > /tmp/test2.sh << EOF
#!/bin/bash
echo "msg1" && ls /wontexists && echo "msg2"
echo "should not see this"
EOF

bash -xe /tmp/test2.sh
echo $?

Uncover the Mystery

set -e only exits on an 'uncaught' error. 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. To be simple, the shell does not exit if the command that fails is part of the command list.

The official explanation for bash -e can be found here. There is a similar discussion in Stack Overflow.

-e

Exit immediately if a pipeline (see
Pipelines), which may consist of a single
simple command (see Simple Commands), a list
(see Lists), or a compound command (see
Compound Commands) 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 !. If a compound command
other than a subshell returns a non-zero
status because a command failed while -e was
being ignored, the shell does not exit. A
trap on ERR, if set, is executed before the
shell exits.

More Reading: Shell Redirect Output To File, And Still Have It On Screen.

Like our blog posts? Discuss with us on LinkedInTwitter, Or Newsletter.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
devops ,bash ,linux ,code

Published at DZone with permission of Denny Zhang, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}