Bash Scripting — Advanced Scripting Techniques
Introduction
Welcome to Chapter 25 — where your Bash scripting skills level up significantly! 🚀
So far you’ve learned variables, loops, functions, arrays, and string manipulation. Now it’s time to explore the advanced plumbing that separates casual scripters from confident shell programmers.
These techniques show up constantly in production scripts, CI/CD pipelines, and system administration tools. Once you master them, you’ll read other people’s scripts with ease — and write cleaner, more powerful ones yourself.
What You’ll Learn in This Chapter
| Topic | What It Covers |
|---|---|
| Here-Documents | Multi-line input with <<EOF, variable expansion, indented heredocs |
| Here-Strings | One-liner input with <<<, parsing tricks, performance benefits |
| Process Substitution | <(cmd) and >(cmd), diff without temp files, avoiding subshell traps |
| Subshells & Command Grouping | () vs {}, isolation, grouped redirection |
| eval, coproc, and exec | Dynamic evaluation, bidirectional processes, file descriptor magic |
| Practice Exercises | 5 hands-on exercises from easy to challenging |
Why These Techniques Matter
Consider this real-world scenario — you need to compare the output of two commands without creating temporary files:
diff <(sort /etc/hosts) <(sort /tmp/hosts.bak)
That single line uses process substitution to avoid temp files entirely. Clean, fast, and elegant.
Or imagine you need to send a multi-line SQL query to a database:
psql -U admin mydb <<EOF
SELECT username, email
FROM users
WHERE created_at > NOW() - INTERVAL '7 days'
ORDER BY created_at DESC;
EOF
Here-documents make this readable and maintainable — no messy string concatenation needed.
Prerequisites
Before diving in, make sure you’re comfortable with:
- ✅ Variables and quoting (Chapter 7-8)
- ✅ Conditionals and loops (Chapter 10-13)
- ✅ Functions (Chapter 17)
- ✅ Pipes and redirection (Chapter 5-6)
- ✅ Arrays (Chapter 21)
If any of those feel shaky, take a quick refresher — these advanced topics build directly on them.
How to Approach This Chapter
- Read each section in order — the concepts build on each other
- Type every example — don’t just read, practice in your terminal
- Break things on purpose — see what happens when you change the syntax
- Do the exercises — they tie everything together
Quick Reference Card
Here’s a cheat sheet you’ll appreciate by the end of this chapter:
# Here-document — multi-line input
cat <<EOF
Hello, $USER
EOF
# Here-string — single-line input
grep "error" <<< "$log_output"
# Process substitution — command as file
diff <(sort file1) <(sort file2)
# Subshell — isolated environment
(cd /tmp && run_risky_command)
# Command grouping — same shell
{ echo "start"; process_data; echo "done"; } > log.txt
# Exec — replace shell / redirect FDs
exec 3>/tmp/mylog.txt
Let’s Get Started!
Head over to the first topic: Here-Documents →
By the end of this chapter, these “advanced” techniques will feel like second nature. Let’s go! 💪