Skip to main content

Bash Scripting

Automate tasks, build tools, and master DevOps with Bash — the default shell on most Linux systems and cloud platforms. From fundamentals to production-grade scripts, these guides cover everything you need.

Learning Path

Start with the fundamentals and progress through advanced topics:

  1. Fundamentals — Variables, quoting, control flow, functions, arrays
  2. Advanced — Process substitution, here documents, traps, debugging, sed/awk, cron
  3. Script Examples — 90+ real-world scripts: sysadmin, networking, math, automation
  4. Cheatsheet — 100+ commands and one-liners for quick reference
  5. Interview Questions — 35+ questions with detailed answers

Quick Start

Your First Bash Script

#!/usr/bin/env bash
echo "Hello, World!"

Save as hello.sh, make executable, and run:

chmod +x hello.sh
./hello.sh

Check Syntax

bash -n script.sh        # Check for errors without running

Common Patterns

Variables and quoting:

name="Alice"
echo "Hello, $name" # Variable expansion
echo 'Literal $name' # No expansion

Conditional logic:

if [ $# -ne 1 ]; then
echo "Usage: $0 <name>"
exit 1
fi

Looping:

for file in *.txt; do
echo "Processing: $file"
done

Functions:

greet() {
echo "Hello, $1!"
}
greet "Alice"

Why Learn Bash?

  • Ubiquitous: Default shell on Linux servers and macOS
  • Essential for DevOps: Docker, Kubernetes, CI/CD automation
  • System Administration: Server management, monitoring, backups
  • Scripting: Task automation, cron jobs, system tools
  • Interview Prep: Common in technical assessments
  • Quick Wins: Automate repetitive tasks immediately

Key Topics

Fundamentals

  • Variables and data types
  • Quoting and expansion
  • Control structures (if/for/while/case)
  • Functions and scope
  • Arrays and associative arrays
  • String manipulation
  • Arithmetic operations
  • Exit codes and error handling

Advanced

  • Process substitution (<(), >())
  • Here documents and here strings
  • Signal handling with trap
  • Debugging (set -x, bash -n)
  • Parameter parsing with getopts
  • Text processing with sed and awk
  • Regular expressions
  • Cron automation
  • Parallel execution

Real-World Use Cases

  • System monitoring scripts
  • Backup automation
  • Log analysis
  • Configuration management
  • Deployment pipelines
  • User provisioning
  • Health checks
PageFocus
FundamentalsCore concepts, syntax, control flow
AdvancedProcess substitution, traps, text processing, automation
Cheatsheet100+ commands, snippets, one-liners
Interview Questions35+ Q&A with detailed explanations

Best Practices

Error Handling

#!/usr/bin/env bash
set -euo pipefail # Exit on error, undefined vars, pipe failures

# Use meaningful error messages
if [[ ! -f "$1" ]]; then
echo "ERROR: File not found: $1" >&2
exit 1
fi

Safe Practices

# Always quote variables
echo "$var" # Good

# Use [[ ]] over [ ]
[[ $var == pattern ]] # Good
[ $var = pattern ] # Old style

# Use local in functions
function my_func() {
local var="local" # Won't affect global scope
}

Debugging

# Enable trace mode
set -x

# Or run with bash -x
bash -x script.sh

# Check syntax
bash -n script.sh

External Resources

Contributing

Have improvements, corrections, or additional examples? Contribute to CloudCaptain and help the community learn Bash!

Next Steps

Ready to dive in? Start with Fundamentals and work through the exercises, or jump to the Cheatsheet for quick reference when you need it.