Controlling Shell Script Failure Behavior in Jenkins Pipelines
This guide explains how to prevent Jenkins shell steps from aborting on errors by using the +e option, appending || true, or custom failure handling commands, and provides four concrete examples with test results to illustrate each method.
Each issue is described with a brief image and text illustrating a Jenkins tip.
Problem
Do not want a Shell script to stop when a command fails.
Want the script to keep running and report the failure.
Solution
Method One
When running a Shell step you can use the built‑in +e option to control error handling, which disables the default "non‑zero exit" abort behavior.
Please refer to the four examples below for test shells and console output.
Example One
When a command returns a non‑zero status, the error is ignored and the script continues.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px">$ <span style="color: #5c2699; line-height: 26px">set</span> +e<br/>$ ls no-exit-file<br/>$ whoami<br/></code>Example Two
If a command returns a non‑zero status, the entire script exits immediately.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px">$ <span style="color: #5c2699; line-height: 26px">set</span> -e<br/>$ ls no-exit-file<br/>$ whoami<br/></code>Method Two
Example Three
Another way is to append || true to the command.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #007400; line-height: 26px"># Command may fail but we ignore the failure</span><br/>$ ls no-exit-file || <span style="color: #aa0d91; line-height: 26px">true</span><br/></code>Example Four
To run a custom action on failure, append || <doSomethingOnFailure>.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #007400; line-height: 26px"># Command may fail and we want to handle the failure</span><br/><span style="color: #007400; line-height: 26px"># If an error occurs, set variable error to true</span><br/>$ ls no-exit-file || error=<span style="color: #aa0d91; line-height: 26px">true</span><br/><br/><span style="color: #007400; line-height: 26px"># Then check the error variable; if true, exit the shell</span><br/>$ <span style="color: #aa0d91; line-height: 26px">if</span> [ <span style="color: #3F6E74; line-height: 26px">$error</span> ]<br/>$ <span style="color: #aa0d91; line-height: 26px">then</span> <br/>$ <span style="color: #5c2699; line-height: 26px">exit</span> -1<br/>$ <span style="color: #aa0d91; line-height: 26px">fi</span><br/></code>DevOps Engineer
DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
