Technical reference
Bash Cheatsheet
Shell commands and dependable automation
Must Know
bash
Safe Script Header
Exit on errors, unset variables, and failed pipelines.
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'bash
Variables and Arguments
Quote expansions unless you explicitly need splitting.
name=${1:-world}
readonly output_dir="./dist"
printf 'Hello, %s\n' "$name"Important Patterns
bash
Conditionals
if [[ -f "$file" ]]; then
printf '%s\n' "Found $file"
elif [[ -d "$file" ]]; then
printf '%s\n' 'Directory'
fibash
Loop Safely
Avoid parsing ls output.
while IFS= read -r line; do
printf '%s\n' "$line"
done < input.txtUseful Recipes
bash
Find and Transform
Null delimiters preserve spaces in filenames.
find src -type f -name '*.ts' -print0 | while IFS= read -r -d '' file; do
printf '%s\n' "$file"
donebash
Temporary Directory
Always clean up temporary resources.
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXITPitfalls & Production
bash
Dangerous Expansion
Empty variables and globs can broaden destructive commands.
# Dangerous: rm -rf $target/*
# Validate and quote exact targets before destructive actions.bash
Debugging
Syntax-check, lint, then trace when needed.
bash -n script.sh
shellcheck script.sh
bash -x script.sh