Bash Scripting Commands & Syntax Matrix
The definitive Bash scripting quick reference: strict mode (set -euo pipefail), parameter expansion, regex matching, line-by-line file loops, and exit traps with 1-click copy.
Shebang & Strict Mode (3 commands)
#!/usr/bin/env bash
Universal bash shebang that finds the bash executable in user PATH.
set -euo pipefail
Bash unofficial strict mode: exits on error (-e), undefined vars (-u), and pipe failures.
IFS=$'\n\t'
Sets Internal Field Separator to newline and tab only, preventing word splitting on spaces.
Variables & Special Parameters (6 commands)
MY_VAR="production"
Assigns a string variable without spaces around the equal sign.
readonly API_KEY="secret"
Declares an immutable constant variable that cannot be reassigned or unset.
echo "$1" "$2" "$@"
Accesses script arguments: $1 is first argument, $2 is second, $@ represents all arguments.
echo "$#"
Returns the total number of arguments passed to the script or function.
echo "$?"
Returns the exit status code of the most recently executed command (0 = success).
echo "$$"
Outputs the Process ID (PID) of the currently running script.
String & Parameter Expansion (6 commands)
${VAR:-"default_value"}
Returns default value if $VAR is unset or empty without mutating VAR.
${VAR:="default_value"}
Sets $VAR to default value if unset or empty, and returns it.
${#VAR}
Returns the character length of the string stored in $VAR.
${FILE%.*}
Strips file extension from filename (removes shortest trailing match of ".*").
${FILE##*.}
Extracts file extension (removes longest leading match up to ".").
${VAR//search/replace}
Replaces all occurrences of "search" pattern with "replace" in $VAR.
Conditionals & Tests (5 commands)
if [[ -f "$FILE" ]]; then ... fi
Tests if $FILE exists and is a regular file.
if [[ -d "$DIR" ]]; then ... fi
Tests if directory exists on filesystem.
if [[ -z "$STRING" ]]; then ... fi
Tests if string is empty (length zero).
if [[ "$NUM" -eq 10 && "$NUM2" -gt 5 ]]; then ... fi
Numerical comparisons: -eq (equal), -ne (not equal), -gt (greater), -lt (less), -ge, -le.
if [[ "$EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then ... fi
Performs regex pattern matching using bash built-in =~ operator.
Loops & Iteration (3 commands)
for file in /path/*.log; do [ -e "$file" ] || continue; echo "$file"; done
Safely iterates over files matching glob pattern, checking existence first.
for i in {1..10}; do echo "Step $i"; done
Generates and iterates through numerical range sequence.
while IFS= read -r line; do echo "$line"; done < input.txt
Line-by-line file reader preserving leading/trailing spaces and backslashes.
Functions & Traps (3 commands)
log_info() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] INFO: $*"; }
Defines a reusable logging function with ISO timestamp.
cleanup() { rm -rf "$TEMP_DIR"; echo "Cleaned up"; }
trap cleanup EXIT
Registers an exit trap handler that executes automatically on script termination.
trap 'echo "Error on line $LINENO"' ERR
Catches any failing command and prints exact file line number where failure occurred.
Redirection & Streams (4 commands)
command > output.log 2>&1
Redirects both standard output (stdout) and standard error (stderr) to file.
command 2>/dev/null
Silences all error messages by sending stderr to /dev/null black hole.
echo "hello" | tee -a script.log
Prints output to terminal while simultaneously appending it to a log file.
cat << 'EOF' > config.yml
server:
port: 8080
EOF
Here-document (heredoc) that writes multi-line string to file.