Dualo
Bash / Shell Scripting

Scripts, shebang & safe defaults

A script is a file of commands. The shebang picks the interpreter; `set -euo pipefail` turns silent bugs into loud failures.

1 min read

A Bash script is a plain text file of commands. To run it: bash script.sh, or make it executable (chmod +x script.sh) and call ./script.sh. The first line — the shebang — tells the kernel which interpreter to use.

Shebang: #!/usr/bin/env bash (portable: finds bash via $PATH). Alternative: #!/bin/bash (hardcoded path — works on Linux, may fail on NixOS/macOS Homebrew paths). For POSIX-only scripts: #!/bin/sh.

Safe mode (top of every serious script): set -euo pipefail — exit on any error (-e), treat unset variables as errors (-u), fail pipelines on any stage failure (-o pipefail). Plus IFS=$'\n\t' to avoid word-splitting bugs. Known as 'unofficial Bash strict mode'.

Why safe mode matters: without -e, a script continues past a failed mkdir, then later cd lands in the wrong place, and rm -rf * runs in your home. With -e, the failed mkdir aborts the script immediately. Catastrophic bugs → loud early failures.

Structure a real script: shebang → set -euo pipefail → helper functions (die(), usage()) → argument parsing → main logic → exit. Add trap cleanup EXIT for cleanup (remove temp files, kill background jobs) on any exit path.

Grounded on https://redsymbol.net/articles/unofficial-bash-strict-mode/

Next up

Signals, jobs & processes

Run commands in the background, wait on them, and handle Ctrl-C gracefully. The runtime side of shell scripting.