---
title: "How to make a Bash script exit on errors (set -euo pipefail)"
description: "Bash keeps running after a command fails. Learn what set -euo pipefail does, where it quietly does not apply, and how trap ERR reports the failing line."
url: https://www.danny.engineering/answer/bash/how-to-make-a-bash-script-exit-on-errors-seteuo-pipefail
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Bash"]
---

# How to make a Bash script exit on errors (set -euo pipefail)

## The short answer


By default, Bash carries on to the next line when a command fails. Put `set -euo pipefail` near the top of your script so it stops on the first failure, treats unset variables as errors, and notices failures inside pipelines. Add an `ERR` trap if you want to know where it stopped.


```bash
#!/usr/bin/env bash
set -Eeuo pipefail
trap 'echo "Error on line $LINENO: $BASH_COMMAND" >&2' ERR

cd /some/build/dir
npm ci
npm run build
```


## Why it happens


Every command returns an exit status: `0` for success, anything else for failure. Bash records it in `$?` but does nothing else with it unless you ask. So a script like this happily deletes files in the wrong place when the `cd` fails:


```bash
cd /tmp/my-build
rm -rf ./*
```


The options change that behaviour:

- **`set -e`** (errexit): exit as soon as a command returns a non-zero status.
- **`set -u`** (nounset): treat expanding an unset variable as an error, instead of silently using an empty string.
- **`set -o pipefail`**: a pipeline's status is that of the last command that failed, rather than always the last command. Without it, `curl https://bad.example | tar xz` only reports `tar`'s status.
- **`set -E`** (errtrace): make an `ERR` trap apply inside functions, command substitutions and subshells too.

## How to fix it


### Start scripts with strict mode


```bash
#!/usr/bin/env bash
set -Eeuo pipefail
```


This is a good default for build scripts, deploy scripts and anything that changes files. It turns silent failures into loud ones.


### Report where it failed with trap ERR


The `ERR` trap runs whenever a command fails in a way that would trigger `set -e`. `$LINENO` and `$BASH_COMMAND` tell you where and what:


```bash
on_error() {
  local status=$?
  echo "Failed with status $status at line $1: $BASH_COMMAND" >&2
}
trap 'on_error $LINENO' ERR
```


Use an `EXIT` trap for cleanup, since it runs however the script ends:


```bash
tmp_dir="$(mktemp -d)"
trap 'rm -rf -- "$tmp_dir"' EXIT
```


### Allow failures on purpose


Some commands are expected to fail. Handle them explicitly so strict mode does not kill the script:


```bash
# Ignore a failure
rm -- old.log || true

# Branch on it
if ! grep -q 'ready' status.txt; then
  echo "Not ready yet"
fi

# Default for an optional variable under set -u
port="${PORT:-3000}"
```


## Common pitfalls


`set -e` has many exceptions, so do not treat it as a guarantee. The ones that catch people most often:

- **Conditions are exempt.** Commands in an `if` or `while` test, or anywhere but the last part of an `&&` or `||` list, do not trigger an exit. That includes everything inside a function you call from an `if`, so `if my_func; then` disables `set -e` for the whole of `my_func`.
- **`local`** **and** **`export`** **hide failures.** In `local out="$(cmd)"` the status is that of `local`, which succeeds. Declare first, then assign: `local out` on one line and `out="$(cmd)"` on the next.
- **Command substitution does not inherit errexit** by default in Bash. A failure in the middle of `$(...)` may not stop the script. Bash 4.4 and later offer `shopt -s inherit_errexit` to change this.
- **Arithmetic can exit unexpectedly.** `((count++))` returns status 1 when `count` was `0`, which ends the script. Use `count=$((count + 1))` instead.
- **`pipefail`** **and early-closing readers.** In `some_command | head -n 1`, `head` may exit first and `some_command` receives SIGPIPE, giving status 141 and stopping the script. Expect this when you cut output short on purpose.
- **`set -u`** **and arrays.** On Bash older than 4.4 (including the 3.2 that ships with macOS), expanding an empty array with `"${arr[@]}"` counts as unset and errors.
- **Sourced scripts.** `set -e` in a file you `source` changes the calling shell too, and can close your interactive terminal on the next failed command.

For anything critical, check important commands explicitly (`cmd || exit 1`) rather than relying only on `set -e`.


## Related reading

- [Bash Reference Manual: The Set Builtin](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)
- [Bash Reference Manual: Bourne Shell Builtins (trap)](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html)
