Shell scripts glue together binaries, APIs, and files. This lesson covers strict mode, pipes, loops, `find` + `xargs`, cron, and when to stop writing Bash and switch to Python or Ansible.
How to read this lesson: Code explorer explains the first lines you should put in every script. Pipeline is the Unix text-processing pattern (filter → transform → aggregate). Tabbed code contrasts safe file iteration with cron’s minimal environment. Grid cards separate Bash sweet spot from cases where a real language wins.
Without `set -euo pipefail`, a failed command inside an `if` test or a pipeline can silently continue—scripts appear to work while half the steps did nothing. pipefail is critical: otherwise only the last command’s exit status counts.
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# ... your commands ...
A pipeline connects stdout of one program to stdin of the next. That is the Unix philosophy: small tools, composable. Use `| tee log.txt` when you need both a file and the next stage.
Pipeline widget: left to right is data flow. grep narrows lines; awk/cut projects columns; sort | uniq -c is the classic frequency count pattern. Redirect at the end captures the final stream—remember `2>&1` to merge stderr for logs.
find /var/log -type f -name '*.log' -print0 | \
xargs -0 -I{} ls -lh {}find . -type f -name '*.txt' -print0 | \
xargs -0 -P 4 gzipBash excels at orchestrating programs that already exist. When you start parsing nested JSON with regex, reach for `jq`, Python, or a task runner—readability and error handling win.