Shell Scripting Deep Dive Cheat Sheet
Advanced Bash scripting patterns including parameter expansion, error handling, arrays, and safe scripting practices.
2 PagesAdvancedJan 28, 2026
Strict Mode & Error Handling
Make scripts fail fast and loudly.
bash
#!/usr/bin/env bashset -euo pipefailIFS=$'\n\t'# -e: exit on any command failure# -u: error on unset variables# -o pipefail: fail if any command in a pipeline failstrap 'echo "Error on line $LINENO" >&2' ERR
Parameter Expansion
Common Bash string/variable manipulations.
bash
name=${1:-default} # Default value if $1 unset/emptyfile=${path##*/} # Strip longest match from front (basename)dir=${path%/*} # Strip shortest match from back (dirname)upper=${str^^} # Uppercase entire stringlen=${#str} # String lengthsub=${str:0:5} # Substring (offset, length)replaced=${str//foo/bar} # Replace all occurrences
Arrays & Loops
Working with indexed arrays and iteration.
bash
arr=(one two three)for item in "${arr[@]}"; do echo "$item"doneecho "Count: ${#arr[@]}"# Read a file line by line safelywhile IFS= read -r line; do echo "$line"done < input.txt
Common Gotchas
Mistakes that cause subtle bugs.
- Unquoted variables- Always quote "$var" to prevent word splitting and glob expansion
- [ vs [[- Prefer [[ ]] in Bash for safer comparisons and pattern matching support
- $(cmd) vs `cmd`- Prefer $() for command substitution; it nests cleanly and is more readable
- local in functions- Declare function variables with 'local' to avoid leaking into global scope
- set -e pitfalls- set -e does not trigger inside conditionals like 'if cmd; then' — errors there must be checked explicitly
- shellcheck- Static analysis tool that catches quoting, portability, and logic bugs before runtime
Pro Tip
Run every script through 'shellcheck' in CI — it catches the exact class of quoting and word-splitting bugs that cause scripts to work in testing but fail unpredictably in production.
Was this cheat sheet helpful?
Explore Topics
#ShellScriptingDeepDive#ShellScriptingDeepDiveCheatSheet#DevOps#Advanced#Strict#Mode#Error#Handling#DataStructures#ErrorHandling#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance