Bash Scripting
Bash shell scripting — variables, conditionals, loops, functions, and common patterns.
Bash is the default shell on Linux and macOS. Writing shell scripts automates repetitive tasks, CI/CD pipelines, and local development workflows. This cheatsheet covers scripting fundamentals with a focus on patterns used in real-world scripts.
All scripts should start with #!/bin/bash and be made executable with chmod +x.
Variables & Substitutions
#!/bin/bashNAME="value"$1, $2, ... $@$(command) or `command`${VAR:-default}Conditionals
if [[ condition ]]; then ... fi[[ -f "$FILE" ]][[ -d "$DIR" ]][[ -z "$VAR" ]][[ "$A" == "$B" ]]Loops
for item in list; do ... donefor ((i=0; i<10; i++)); do ... donewhile read line; do ... done < fileFunctions
func_name() { ... }local VAR="value"return nFile Operations
cp -r src destmkdir -p path/to/dirrm -rf pathError Handling & Debugging
set -eset -uset -xtrap "cmd" EXITtrap "cmd" ERRBash Scripting
Bash shell scripting — variables, conditionals, loops, functions, and common patterns.
Bash is the default shell on Linux and macOS. Writing shell scripts automates repetitive tasks, CI/CD pipelines, and local development workflows. This cheatsheet covers scripting fundamentals with a focus on patterns used in real-world scripts.
All scripts should start with #!/bin/bash and be made executable with chmod +x.
Variables & Substitutions
#!/bin/bash — Shebang — tells the system to use bash to execute the script.NAME="value" — Assign a variable. No spaces around =. Reference with $NAME or ${NAME}.$1, $2, ... $@ — Positional parameters — arguments passed to the script or function.$(command) or `command` — Command substitution — capture command output into a variable.${VAR:-default} — Use default value if VAR is unset or empty.Conditionals
if [[ condition ]]; then ... fi — Test condition with [[ ]] (bash-specific, more powerful than [ ]).[[ -f "$FILE" ]] — Test if a file exists and is a regular file.[[ -d "$DIR" ]] — Test if a directory exists.[[ -z "$VAR" ]] — Test if a variable is empty (zero-length string).[[ "$A" == "$B" ]] — String comparison inside [[ ]]. Use != for inequality.Loops
for item in list; do ... done — Iterate over a list of items.for ((i=0; i<10; i++)); do ... done — C-style for loop with increment.while read line; do ... done < file — Read a file line by line.Functions
func_name() { ... } — Define a bash function.local VAR="value" — Declare a local variable inside a function. Does not leak to global scope.return n — Return a numeric exit code from a function (0=success, 1-255=error).File Operations
cp -r src dest — Copy files or directories recursively.mkdir -p path/to/dir — Create a directory hierarchy. No error if already exists.rm -rf path — Remove a file or directory recursively and forcefully.Error Handling & Debugging
set -e — Exit immediately if any command exits with a non-zero status.set -u — Treat unset variables as an error and exit.set -x — Print each command before executing it (debug mode).trap "cmd" EXIT — Run a command when the script exits, even on error.trap "cmd" ERR — Run a command when any command exits with non-zero status.