set -euo pipefail in bash

Basics

When asking GenAI to write a bash script, the following line is often added to a script at the top:

set -euo pipefail

This is a shorthand for three separate options that change how bash handles errors. Each option is explained below.

set -e: exit on error

By default, bash continues running even if a command fails. set -e tells bash to exit immediately if any command returns a non-zero exit code. Exit codes are numbers that commands return when they finish: 0 means success, anything else means something went wrong.

set -e

cat file_that_does_not_exist.txt   # this fails
echo "This line should only print if cat succeeded"    # bash exits before reaching this

To follow this behavior generate a file called test_set.sh, add the code from above run it with bash test_set.sh. Try this with and without using the line with set.

With set included should return:

cat: file_that_does_not_exist.txt: No such file or directory

Removing set -e runs:

cat: file_that_does_not_exist.txt: No such file or directory
This line should only print if cat succeeded

set -u: treat unset variables as errors

By default, bash silently substitutes an empty string when a variable is not set. set -u makes bash exit with an error instead.

set -u

echo "$UNDEFINED_VARIABLE"   # exits with: unbound variable

This is particularly relevant when combined with file checks. For example, if a variable holding a filename is accidentally unset, set -u will catch it before the variable is passed to a command.

Running these lines above with test_set.sh should return:

test_set.sh: line 3: UNDEFINED_VARIABLE: unbound variable

while not including set returns an empty line.

set -o pipefail: catch failures in pipes

By default, a pipeline/pipe returns the exit code of the last command only. If in the example below cat fails but sort succeeds, bash records the pipeline as successful (exit code 0) and the failure from cat is invisible to bash. set -o pipefail changes this: the pipeline now returns the exit code of the last (rightmost) command that failed, or 0 if all commands worked.

In the example below, sort will run in both cases. That is because pipefail does not stop the script. What changes is what bash records as the result of the pipeline, which we made visible by printing $?.

Note: Since pipefail alone does not stop execution that becomes set -e’s job. pipefail’s role is to ensure bash has accurate information that set -e can act upon. Specifically, this ensures that a failure upstream is not silently swallowed before set -e can act on it. Therefore you usually use these two options together with each other, or you use pipefail with || exit 1 after each pipe (see pipefail without set -e below).

set -o pipefail

cat file_that_does_not_exist.txt | sort

# Print the error code with `$?` to understand the difference when running with/without set
# echo "Exit code: $?" must immediately follow the pipeline — any command in between resets $?.
echo "Exit code of pipeline: $?" 

Adding these lines into test_set.sh should return:

cat: file_that_does_not_exist.txt: No such file or directory
Exit code of pipeline: 1

while not including set returns:

cat: file_that_does_not_exist.txt: No such file or directory
Exit code of pipeline: 0

We see that both times the code runs but returns different exit codes.

Putting it all together

In a normal script we would write something like this:

set -euo pipefail
cat file_that_does_not_exist.txt | sort
echo "Script continued after failed pipeline" 

which returns:

cat: file_that_does_not_exist.txt: No such file or directory

Without set we would get:

cat: file_that_does_not_exist.txt: No such file or directory
Script continued after failed pipeline

This section is for students working on an HPC. If you are not yet running jobs on an HPC, you can skip this section.

Slurm job scripts are bash scripts, so the same default behavior applies: without set -euo pipefail, bash will not exit on errors and will silently swallow pipeline failures. However, Slurm adds an important layer: the job’s reported status (COMPLETED vs. FAILED) depends on the final exit code of the script as a whole, not on the exit codes of individual commands within it. This means a job can report COMPLETED even if internal steps failed silently, which can be difficult to detect when running long jobs.

Testing on Crunchomics

We can check the exact behavior on the UvA HPC by creating a file called test_pipefail_slurm.sh:

#!/bin/bash
#SBATCH --job-name=test_pipefail
#SBATCH --output=test_pipefail_%j.out
#SBATCH --error=test_pipefail_%j.err
#SBATCH --time=00:01:00
#SBATCH --ntasks=1

# --- WITHOUT pipefail ---
echo "=== Without pipefail ==="
cat file_that_does_not_exist.txt | sort
echo "Exit code of pipeline: $?"
echo "Script continued"

# --- WITH pipefail and set -e ---
echo "=== With pipefail and set -e ==="
set -euo pipefail
cat file_that_does_not_exist.txt | sort
echo "Exit code of pipeline: $?"
echo "Script continued after pipefail"

Submit this with sbatch test_pipefail_slurm.sh.

This will generate two files:

  • test_pipefail_<JOBID>.out — stdout (what echo prints)
  • test_pipefail_<JOBID>.err — stderr (what cat prints when it fails)

Important: You want to check both files to not miss errors!

The .err file contains (we see that error appears twice because cat fails in both the without and with pipefail sections):

cat: file_that_does_not_exist.txt: No such file or directory
cat: file_that_does_not_exist.txt: No such file or directory

The .out file contains:

=== Without pipefail ===
Exit code of pipeline: 0
Script continued

=== With pipefail and set -e ===

Note that echo “Script continued after pipefail” never prints. That is because set -estopped the script when pipefail exposed the non-zero exit code.

Once the job completes, check what Slurm recorded with sacct -j <JOBID> --format=JobID,JobName,ExitCode,State (replace the jobID with the job number from your script). For how to use sacct, see this section. This should return something like:

JobID           JobName ExitCode      State
------------ ---------- -------- ----------
264615       test_pipe+      1:0     FAILED
264615.batch      batch      1:0     FAILED
264615.exte+     extern      0:0  COMPLETED

Without set -e and pipefail, the same sacct would however return:

JobID           JobName ExitCode      State
------------ ---------- -------- ----------
264524       test_pipe+      0:0  COMPLETED
264524.batch      batch      0:0  COMPLETED
264524.exte+     extern      0:0  COMPLETED

What this demonstrates: without both pipefail and set -e active, a pipeline failure propagates nowhere. It is invisible inside the .out file (the error went to .err), and invisible to Slurm (the job reports COMPLETED). The failure existed, but left no trace that would trigger any alert or downstream check.

Where to put set in a job script

A set option only works from its own line down. Commands above the set line run with the default behavior of bash. This matters in Slurm job scripts that use conda, because they often start with these two lines:

source ~/.bashrc
conda activate fastp_1.4.0

source ~/.bashrc runs your .bashrc file. This file holds your personal settings, such as where conda is installed. We use it in job scripts so that Slurm can find conda.

If set -u comes before source ~/.bashrc, the job can stop at the .bashrc. I tested three orders on Crunchomics, each with the fastp_1.4.0 and the checkm2_1.1.0 environment.

1. set before source ~/.bashrc: the job fails

set -euo pipefail
source ~/.bashrc
conda activate fastp_1.4.0

The .err file contains:

/home/<user>/.bashrc: line 19: PS1: unbound variable

and sacct reports FAILED with exit code 1:0. The script never reached conda activate.

PS1 is the variable that sets how your command prompt looks. Bash only sets it when you type commands yourself in a terminal. A Slurm job runs without a terminal, so PS1 is not set. The .bashrc on Crunchomics contains this line, which changes the look of the prompt:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

This line reads PS1, and set -u stops the script there because PS1 is not set. A comment in the .bashrc says that a Crunchomics setup script added this line, so your .bashrc probably has it too. If you added your own lines to your .bashrc, the line number can differ, or the error can come from another variable.

2. set after source ~/.bashrc, before conda activate: the job works

source ~/.bashrc
set -euo pipefail
conda activate fastp_1.4.0

sacct reports COMPLETED with exit code 0:0, and the .err file is empty. So conda activate itself works with set -u on.

3. set after conda activate: the job works

source ~/.bashrc
conda activate fastp_1.4.0
set -euo pipefail

sacct reports COMPLETED with exit code 0:0, and the .err file is empty.

Recommendation: put set after source ~/.bashrc. Putting it after conda activate is also fine. set -o pipefail alone does not cause this problem, because the error comes from set -u.

Important: Activation scripts of other conda environments can behave differently. If a job stops with unbound variable right after conda activate, move set below the conda activate line.

Hidden rules of set -e

set -e does not stop at every failure, and it can stop a script before code that you wanted to run. Two things are easy to miss.

In an && chain, only the last command can stop the script. && between two commands means: run the second command only if the first one worked. Save this as test_chain.sh:

set -e
ls missing_file.txt && echo "ls worked"
echo "Script continued after the && chain"

Run it and print its exit code:

bash test_chain.sh
echo "exit code: $?"

This prints:

ls: cannot access 'missing_file.txt': No such file or directory
Script continued after the && chain
exit code: 0

ls fails, so echo "ls worked" is skipped. But set -e ignores a failure of a command that is not the last one in an && chain. So the script goes on and ends with exit code 0. If you swap the two commands (echo "start" && ls missing_file.txt), the failing command is the last one, and the script stops.

Code that should run after a failure is never reached. Some scripts check $? after a tool and write the failed sample to a list. Save this as test_failed_list.sh:

set -e
ID="sample2"
ls "${ID}_R1.fastq.gz"
if [[ $? -ne 0 ]]; then
    echo "$ID" >> failed_samples.txt
fi
echo "Script finished"

Run it, then look for the list of failed samples:

bash test_failed_list.sh
ls failed_samples.txt

This prints:

ls: cannot access 'sample2_R1.fastq.gz': No such file or directory
ls: cannot access 'failed_samples.txt': No such file or directory

set -e stops the script at the first ls, so the if block never runs and failed_samples.txt is never made. An empty or missing list of failed samples then looks as if every sample worked. Without set -e, the same script works: it reaches the if block and writes sample2 into the list.

pipefail without set -e

You can use set -o pipefail alone and stop the script yourself with || exit 1 after a pipe. || between two commands means: run the second command only if the first one fails. Save this as test_pipe.sh:

set -o pipefail
ls missing_file.txt | gzip > out.txt.gz || exit 1
echo "Script continued"

Run it and print its exit code:

bash test_pipe.sh
echo "exit code: $?"

This prints:

ls: cannot access 'missing_file.txt': No such file or directory
exit code: 1

Remove the set line and run the same two commands again:

ls: cannot access 'missing_file.txt': No such file or directory
Script continued
exit code: 0

Without pipefail, || exit 1 only sees the exit code of gzip, and gzip worked. Note that in both runs the failed pipe still leaves an out.txt.gz behind. It is about 20 bytes, a gzip file with nothing in it. This is why a check with -s is not enough after a failed job, see Why the marker file works.

With this pattern you decide where the script stops, and you can see it in the script. Skip finished samples safely gives a job script template built on it.

When it helps and when it causes problems

These options are standard practice in production scripts and are not wrong. But each one has a catch:

  • set -o pipefail is safe and useful when your script has a pipe. It does not stop anything by itself, it only makes sure a failure in a pipe is not hidden
  • set -e stops the script at a failure, but not at every failure, and it can stop the script before code that you wanted to run. See Hidden rules of set -e
  • set -u stops the script at a variable that is not set. This is useful, but on Crunchomics it also stops a job at your .bashrc, see Where to put set in a job script

Even more important: In computational biology non-zero exit codes are sometimes expected and you might want that the script continues as they don’t always mean something went wrong. A common example is grep: it returns exit code 1 when no match is found, and exit code 2 only when an actual error occurs (e.g. the file does not exist). With set -e active, a grep that finds no match will kill the script, even though “no match found” is a perfectly valid biological result as seen in the example below:

set -euo pipefail

# Create a dummy annotation file for showcasing the command
touch annotations.txt

# This kills the script if the gene is absent from the file
# even though absence is a meaningful biological result 
# since it might tell us that a genome does not encode the gene we are looking for
grep "gene_of_interest" annotations.txt

echo "Pipeline is finished"

grep produces no output when there is no match. The script exits silently with code 1, therefore not printing Pipeline is finished.

Removing the set command returns:

Pipeline is finished

With || exit 1 instead of set -e, you choose which commands may stop the script. You add it after each tool command, and you leave it out after a grep where “no match” is a valid result.

Recommendations:

  • For your own job scripts, use set -o pipefail and put || exit 1 after every tool command, without set -e. You then see in the script where it can stop. The template on Skip finished samples safely is built this way
  • If you get a script with set -euo pipefail, for example from a GenAI tool, change that line to set -o pipefail and put || exit 1 after every tool command. Always do both. A script without set -e and without || exit 1 goes on after every failure, which is worse than before. You can ask the GenAI tool to make this change, but then read the script and check that every tool command ends with || exit 1
  • This change also removes set -u. The template replaces it with a check for an empty sample ID, which is the variable that matters most in a job script
  • If a script with set -euo pipefail stops without a clear message, this line is the first place to look

Whichever option you choose, an exit code of 0 only tells you that no command reported a failure. It does not tell you that the results are correct. So after each step, look at the outputs, see Checking the outputs.

A note on GenAI-generated scripts: GenAI tools often add set -euo pipefail because it is considered best practice for production scripts. That is not wrong, but as shown above, set -e can stop a script where you don’t expect it, or not stop it where you do. Replace it with set -o pipefail and || exit 1 after every tool command, as described in the recommendations above. For more patterns to look for in GenAI code, see Checking GenAI-generated scripts.