Over a million developers have joined DZone.

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

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

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.

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

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.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}