Cheat SheetsLinuxShell

Shell — Cheat Sheet

Linux · 2 topics. Download the PDF or the Instagram carousel and share it.

Cheat Sheet · AiCanCode.org
Shell
Linux2 topicsQuick revision reference
1

Shell Scripting — Bash Fundamentals

Bash scripts automate repetitive tasks, deploy software, and glue tools together. Master variables, conditionals, loops, functions, and error handling to write production-grade scripts.

  • Always start scripts with #!/usr/bin/env bash and set -euo pipefail.
  • Use [[ ]] instead of [ ] — it supports &&, ||, regex, and does not split on spaces.
  • Quote all variable expansions: "$VAR" not $VAR — prevents word splitting on spaces.
  • Use local keyword inside functions to avoid polluting global scope.
  • $? is the exit code of the last command; 0 = success, non-zero = failure.
  • Redirect errors to stderr with >&2 so they can be separated from normal output.
bash — script skeleton with strict mode
#!/usr/bin/env bash
# ↑ shebang: tells OS which interpreter to use (/usr/bin/env finds bash in PATH)

set -e          # exit immediately on any error (non-zero exit code)
set -u          # treat unset variables as errors (instead of empty string)
set -o pipefail # pipe fails if ANY command in the pipe fails (not just last)
# Shorthand: set -euo pipefail

# Debugging mode (print every command before executing)
set -x          # enable debug tracing
set +x          # disable debug tracing

# Script info
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_NAME="$(basename "$0")"

# Trap for cleanup on exit
cleanup() {
  echo "Cleaning up..."
  rm -f /tmp/my-lock-file
}
trap cleanup EXIT    # runs cleanup() on any exit (normal or error)
trap 'echo "Error at line $LINENO"' ERR  # print line number on error

echo "Script started from: $SCRIPT_DIR"
2

Text Processing — grep, awk, sed, cut, sort

Linux text processing tools — grep, awk, sed, cut, sort, uniq — are the foundation of log analysis, data extraction, and pipeline automation. Combining them with pipes creates powerful one-liners.

  • grep filters lines; awk processes fields; sed transforms text; cut extracts columns.
  • Pipe | is the glue: each tool's stdout becomes the next tool's stdin.
  • sort | uniq -c | sort -rn gives you a frequency count for any text field.
  • grep -E (extended regex) supports | (OR), + (one or more), ? (zero or one).
  • awk's END block runs once after all lines — use for totals and summaries.
  • Always use grep -v to exclude noise before grep-ing for the signal.
bash — grep for log analysis
# Basic grep
grep "error" /var/log/nginx/error.log           # lines containing "error"
grep -i "error" /var/log/syslog                 # case-insensitive
grep -v "DEBUG" app.log                         # lines NOT matching
grep -n "error" app.log                         # show line numbers
grep -c "error" app.log                         # count matching lines
grep -l "error" /var/log/*.log                  # just file names

# Context
grep -A 3 "OutOfMemoryError" app.log            # 3 lines AFTER match
grep -B 2 "OutOfMemoryError" app.log            # 2 lines BEFORE match
grep -C 5 "exception" app.log                   # 5 lines around match

# Extended regex (grep -E or egrep)
grep -E "ERROR|FATAL|CRITICAL" app.log          # OR
grep -E "^2024-01-1[5-8]" app.log               # lines starting with date range
grep -E "[0-9]{1,3}(.[0-9]{1,3}){3}" access.log # IP addresses

# Recursive search in files
grep -r "password" /etc/ 2>/dev/null            # search all files in /etc
grep -rl "TODO" /opt/myapp/src/                 # just file names

# Combined with other tools
cat /var/log/nginx/access.log | grep " 500 " | wc -l    # count 500 errors
grep "ERROR" app.log | grep "2024-01-15"        # errors on specific date
Learn this free with Aria, your AI tutor → AiCanCode.org/learn/linux