Luke Oliff.

TIL: Using set -e and set -x in Bash Scripts

·TIL·1 min read·Luke Oliff

Shell scripts fail silently by default. set -e and set -x fix that.

#!/bin/bash
set -e
set -x

echo "Downloading model..."
curl -O https://example.com/model.bin
echo "Running transcription..."
python transcribe.py model.bin
echo "Done"

set -e exits on any non-zero exit code. No more running through the rest of the script after curl failed. set -x prints every command before executing it.

#!/bin/bash
set -ex

Combine both. Every script I write starts with set -ex.

Does set -e handle pipes?

set -e only checks the exit code of the last command in a pipe. set -o pipefail makes it fail if any command in the pipe fails.