Bash
October 22

5 Bash Tricks That Will Make You a Better SRE

When an incident hits production, time slows down — and every second counts. Dashboards start flashing red, CPU usage spikes across clusters, logs grow by the megabyte, and alerts flood your Telegram channel.

In that chaos, your terminal becomes your battlefield — and how you use Bash determines whether you’re firefighting or fixing.

Most engineers know basic Bash commands, but great SREs know how to bend the shell to their will. Here are five Bash tricks that separate seasoned SREs from everyone else — the kind that turn a 30-minute debug session into a five-minute win.

1. Process Substitution — Compare Anything, Anywhere, Instantly

Think beyond pipes. Process substitution (<(...)) lets you treat live command outputs as files — perfect for real-time comparisons.

# Compare logs from two servers
diff <(ssh server1 "tail -f /var/log/app.log") <(ssh server2 "tail -f /var/log/app.log")

# Monitor two pods side by side
paste <(kubectl logs -f pod1) <(kubectl logs -f pod2) | column -t

# Compare configs across environments
diff <(curl -s https://prod.api.com/config) <(curl -s https://staging.api.com/config)

This feature feels like magic: no temp files, no manual juggling — just instant, live insight.

2. Command History — Your Incident Time Machine

When things break, there’s no time for retyping long commands. Bash history expansion gives you superhuman speed:

!kubectl       # Repeat the last kubectl command
^tpyo^typo     # Fix a typo from the previous command
sudo !!        # Rerun the last command with sudo
cp /var/log/app.log !$   # Use the last argument again

Pro tip: Add this to your `.bashrc` to preview what history expansion will run:

bind Space:magic-space

Now hitting the spacebar after !kubectl shows exactly what Bash will execute — a lifesaver during high-stress debugging.

3. Brace Expansion — Batch Operations Without Scripts

Brace expansion {} is like having loops baked right into Bash syntax.

# Create backups
mkdir backup-{db,logs,config}-$(date +%Y%m%d)

# Check health for multiple environments
for env in {prod,staging,dev}; do
  echo "=== $env ==="
  curl -s https://$env.api.com/health | jq '.status'
done

# Restart multiple hosts
for host in web-{01..05}; do
  ssh $host "sudo systemctl restart nginx"
done

With this, you automate repetitive operations in one line — no need for temporary scripts or overcomplicated loops.

4. Command Substitution With Error Handling — Smart and Safe Automation

Command substitution $(...) lets your scripts react to live data. Pair it with simple error handling, and you get safe, self-correcting automation:

if pod_name=$(kubectl get pods -l app=critical --no-headers 2>/dev/null); then
  echo "Found pod: $pod_name"
  kubectl describe pod $pod_name
else
  echo "❌ No pods found - checking deployments"
  kubectl get deployments -l app=critical
fi

Why it matters: instead of hardcoding names and assumptions, your commands adapt to the current system state — a must for dynamic, ever-changing environments.

5. Parameter Expansion — Config Without Config Managers

This is Bash’s built-in templating system — perfect for defaults, fallbacks, and string manipulation.

DATABASE_URL=${DATABASE_URL:-postgresql://localhost:5432/app}
LOG_LEVEL=${LOG_LEVEL:-INFO}
TIMEOUT=${TIMEOUT:-30}

Or extract and manipulate values:

config="/etc/myapp/prod/database.conf"
env=${config%/*}; env=${env##*/}   # -> "prod"

db_url="postgresql://user:secret@db.example.com:5432/app"
safe_url=${db_url//:*@/:xxx@}      # -> hides the password

With this, you can handle complex configuration logic right inside Bash — no Python, no jq, no sed needed.

Bonus: Instant System Dashboard

Need a quick status view during an outage? One command:

watch -n1 '
echo "=== CPU/MEMORY ==="
top -bn1 | head -5
echo -e "\n=== DISK USAGE ==="
df -h | grep -v tmpfs
echo -e "\n=== NETWORK ==="
ssh -tuln | grep LISTEN | head -5
echo -e "\n=== RECENT ERRORS ==="
tail -n3 /var/log/syslog | grep -i error
'

This gives you a live, auto-updating dashboard — CPU, memory, disk, network, and errors — refreshed every second.

Why These Tricks Matter

SREs operate under pressure, often on unfamiliar systems. These Bash habits give you:

- Speed — fewer keystrokes, faster recovery.
- Reliability — fewer manual errors.
- Portability — works everywhere Bash does.
- Scalability — handle multiple servers and services simultaneously.

Final Thoughts

These aren’t just “cool hacks” — they’re battle-tested techniques that define a senior SRE’s command-line fluency. Master them, and you’ll turn Bash from a basic tool into an extension of your engineering intuition. Next time your dashboard lights up like a Christmas tree — you’ll be ready.