Shebang
Bash · Reference cheat sheet
Shebang
Bash · Reference cheat sheet
📋 Overview
The shebang (#!) is the first line of a script. It tells the kernel which interpreter to run. Prefer #!/usr/bin/env bash so the script finds bash on PATH across systems.
🔧 Core concepts
| Form | Meaning |
|---|---|
#!/bin/bash | Hard-coded path to bash |
#!/usr/bin/env bash | Resolve bash via PATH (portable) |
#!/bin/sh | POSIX sh (often dash on Debian) |
#!/usr/bin/env python3 | Same pattern for other languages |
Requirements: shebang must be line 1, no BOM, Unix line endings (LF). The file needs execute permission (chmod +x). Options after the interpreter (e.g. #!/usr/bin/env bash -e) are not portable with env on all systems—set options inside the script instead.
💡 Examples
Portable bash script:
#!/usr/bin/env bash
set -euo pipefail
echo "Running under: $BASH_VERSION"Make executable and run:
chmod +x ./deploy.sh
./deploy.sh # uses shebang
bash ./deploy.sh # ignores shebang; forces bashDetect interpreter:
head -n1 "$0" # show shebang of current script
readlink -f "$(command -v bash)"Avoid:
#!/usr/bin/env bash -e # often broken: env treats "-e" as a program nameUse set -e in the body instead.
⚠️ Pitfalls
- Windows editors may save
CRLF; the kernel then looks for/usr/bin/env bash\rand fails. #!/bin/bashbreaks on systems where bash lives elsewhere (e.g. some BSDs, Nix).- Running with
sh script.shforces POSIXshand ignores bashisms even if the shebang says bash. envmay not pass flags; putset -o/set -euo pipefailafter the shebang.- Scripts without a shebang inherit the caller’s shell when run as
./scriptonly if the kernel allows—always include one.