Skip to content

skill-shell-script-no-error-handling

Shell script lacks error handling (set -e or set -euo pipefail)

Warning Fixable

Rule Details

Without proper error handling, shell scripts continue executing after a command fails, which can cause cascading issues and data corruption. This rule checks .sh and .bash files for the presence of set -e, set -euo pipefail, or a trap ... ERR handler. If none are found, a warning is reported with an auto-fix that inserts set -euo pipefail after the shebang line.

Incorrect

Script with no error handling

bash
#!/bin/bash
echo "Building..."
npm run build
echo "Deploying..."
npm run deploy

Correct

Script with set -euo pipefail

bash
#!/bin/bash
set -euo pipefail
echo "Building..."
npm run build
echo "Deploying..."
npm run deploy

Script with trap-based error handling

bash
#!/bin/bash
trap 'echo "Error on line $LINENO"; exit 1' ERR
npm run build

How To Fix

Add set -euo pipefail immediately after the shebang line. This causes the script to exit on any command failure (-e), undefined variable usage (-u), and pipe failures (-o pipefail). The auto-fixer inserts this line automatically.

Options

This rule does not have any configuration options.

Resources

Version

Available since: v0.2.0