Founding prices until 15 December: 30% off Pro, locked for life. See pricing

$ … unbound variable

Bash 'unbound variable': set -u and how to handle optional values

The script uses set -u and read a variable that was never set.

Shell

What it means

With set -u (or set -euo pipefail), bash stops at the first use of an unset variable instead of silently using an empty string. That's usually what you want: it found a missing setting or a typo.

Causes and fixes

Most likely first. Run the check, and if it matches, apply the fix.

1. A required setting isn't provided (missing env var or CI secret)

Check:

Look at the line and variable named in the error

Fix: Check required settings up front: : "${DB_URL:?DB_URL is required}".

2. The value is optional

Check:

grep -n VAR script.sh

Fix: Use a default: "${VAR:-default}" (or "${VAR-}" for empty).

3. Positional argument missing ($1 unbound)

Check:

Count the arguments the script was called with

Fix: Check $# and print usage.

Fix it yourself

A live machine in your browser with this problem already on it. The grader checks the result.

Open the lab

No account needed for your first tries.