Skip to content

Introduction

Bash (Bourne Again SHell) is a Unix shell and command language that serves as the default login shell for most Linux distributions and macOS. It’s a powerful command-line interpreter that combines features from sh (Bourne shell), ksh (Korn shell), and csh (C shell).

  1. Command Interpreter: Executes commands entered by the user
  2. Scripting Language: Allows writing scripts for automation
  3. Job Control: Manages background and foreground processes
  4. History: Maintains command history with arrow keys
  5. Tab Completion: Auto-completes commands and filenames
  6. Extensible: Supports aliases, functions, and plugins

┌─────────────────────────────────────────────────────────────────────┐
│ BASH HISTORY TIMELINE │
└─────────────────────────────────────────────────────────────────────┘
1989 ─────────────────────────────────────────────────────────────> 2024
│ │
│ │
▼ ▼
Bash 1.0 Bash 5.2
Released Latest
Version
- First public release │
- Based on Bourne Shell │
- Added new features - Improved
- POSIX compliance - Better
performance
- New features
VersionYearKey Features
Bash 1.01989Initial release
Bash 2.01996Arrays, improved pattern matching
Bash 3.02005Regular expression support
Bash 4.02009Associative arrays, case modification
Bash 5.02019Improved array handling, coprocesses
Bash 5.22024Latest stable version

In today’s DevOps and SRE environments, Bash scripting remains a critical skill despite the emergence of many automation tools. Here’s why:

┌─────────────────────────────────────────────────────────────────────┐
│ BASH FOR DEVOPS ENGINEERS │
└─────────────────────────────────────────────────────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ CI/CD Pipeline │ │ Infrastructure │ │ Deployment │
│ Creation │ │ Automation │ │ Scripts │
│ │ │ │ │ │
│ • Jenkins │ │ • Terraform │ │ • Rollback │
│ • GitLab CI │ │ • Cloud-Init │ │ • Health Checks │
│ • GitHub │ │ • User Data │ │ • Service Restart │
│ • AWS CodeBuild│ │ • Ansible │ │ • Deployments │
└─────────────────┘ └─────────────────┘ └─────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Monitoring & │ │ Log Analysis │ │ Container │
│ Alerting │ │ │ │ Orchestration │
│ │ │ │ │ │
│ • Custom │ │ • Parsing │ │ • Docker │
│ Metrics │ │ • Aggregation │ │ • Kubernetes │
│ • Alerts │ │ • Reporting │ │ • Helm │
│ • Dashboards │ │ • Logrotate │ │ • Container Cleanup│
└─────────────────┘ └─────────────────┘ └─────────────────────┘

SRE (Site Reliability Engineers) Use Cases

Section titled “SRE (Site Reliability Engineers) Use Cases”
┌─────────────────────────────────────────────────────────────────────┐
│ BASH FOR SREs │
└─────────────────────────────────────────────────────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Incident │ │ Chaos │ │ Post-Mortem │
│ Response │ │ Engineering │ │ Analysis │
│ │ │ │ │ │
│ • Runbooks │ │ • Experiments │ │ • Log Collection │
│ • Automation │ │ • Fault │ │ • Root Cause │
│ • Recovery │ │ Injection │ │ • Metrics │
│ • Escalation │ │ • Chaos Monkey │ │ • Reporting │
└─────────────────┘ └─────────────────┘ └─────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Capacity │ │ SLO/SLI │ │ On-Call │
│ Planning │ │ Monitoring │ │ Tools │
│ │ │ │ │ │
│ • Forecasting │ │ • Measurement │ │ • Alerts │
│ • Scaling │ │ • Alerts │ │ • Escalation │
│ • Optimization│ │ • Reporting │ │ • Documentation │
└─────────────────┘ └─────────────────┘ └─────────────────────┘
┌─────────────────────────────────────────────────────────────────────┐
│ BASH FOR SYSADMINS │
└─────────────────────────────────────────────────────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Server │ │ User │ │ Backup & │
│ Provisioning │ │ Management │ │ Recovery │
│ │ │ │ │ │
│ • Setup │ │ • Creation │ │ • Automated │
│ • Configuration│ │ • Permissions │ │ Backups │
│ • Maintenance │ │ • Auditing │ │ • Restore │
│ • Patching │ │ • Password mgmt │ │ • Verification │
└─────────────────┘ └─────────────────┘ └─────────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Security │ │ Performance │ │ Patch │
│ Hardening │ │ Tuning │ │ Management │
│ │ │ │ │ │
│ • Auditing │ │ • Monitoring │ │ • Updates │
│ • Compliance │ │ • Optimization │ │ • Rollback │
│ • Monitoring │ │ • Troubleshooting│ │ • Validation │
│ • Intrusion │ │ • Profiling │ │ • Testing │
│ Detection │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────────┘

Featureshbashzshfish
POSIX CompliantPartial
Arrays
Associative Arrays✓ (4+)
Extended Globbing
Auto-completionBasicGoodExcellentExcellent
Interactive FeaturesBasicGoodExcellentExcellent
Script CompatibilityPartialPartial
┌─────────────────────────────────────────────────────────────────────┐
│ CHOOSING THE RIGHT SHELL │
└─────────────────────────────────────────────────────────────────────┘
┌──────────────┐
│ Need a │
│ Shell? │
└──────┬───────┘
┌────────────┼────────────┐
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Script │ │Interac- │ │Advanced │
│ Writing │ │tive Use │ │Features │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ sh │ │ zsh │ │ fish │
│ (port- │ │ (best │ │ (easy │
│ able) │ │ fea- │ │ learn, │
│ │ │ tures) │ │ modern)│
└─────────┘ └─────────┘ └─────────┘
┌─────────────┐
│ For DevOps/ │
│ SRE: │
│ Use bash │
│ (industry │
│ standard) │
└─────────────┘

Terminal window
# Check bash version
bash --version
# Check if running in bash
echo $BASH_VERSION
# Check shell
echo $SHELL
# List available shells
cat /etc/shells
$ bash --version
GNU bash, version 5.2.37 (x86_64-pc-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Bash supports emacs and vi-style editing modes:

Terminal window
# Set emacs mode (default)
set -o emacs
# Set vi mode
set -o vi
# Check current mode
set -o | grep -i emacs
# Useful shortcuts in emacs mode:
# Ctrl+A - Move to beginning of line
# Ctrl+E - Move to end of line
# Ctrl+K - Kill text to end of line
# Ctrl+U - Kill text to beginning of line
# Ctrl+Y - Yank (paste) killed text
# Ctrl+R - Reverse search history
Terminal window
# Run in background
./script.sh &
# List jobs
jobs
# Bring to foreground
fg %1
# Send to background
Ctrl+Z
bg %1
# Kill background job
kill %1
Terminal window
# Previous command
!!
# Last argument
!$
# Second-to-last argument
!:2
# Command history
history
# Reverse search
Ctrl+R
# Execute last command starting with 'git'
!git
Terminal window
# Match files
ls *.txt
ls file?.log
ls file[0-9]*
# Extended globbing
shopt -s extglob
ls !(pattern).txt # All except pattern
ls @(a|b).txt # a.txt or b.txt
ls *(a).txt # Zero or more
ls +(a|b).txt # One or more
ls?(a).txt # Zero or one
Terminal window
# Basic here document
cat << EOF
Multi-line
text input
EOF
# With variable substitution
cat << EOF
Date: $(date)
Home: $HOME
EOF
# Without expansion (quoted delimiter)
cat << 'EOF'
This is literal: $HOME
EOF

┌─────────────────────────────────────────────────────────────────────┐
│ BASH REAL-WORLD USES │
└─────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────┐
│ 1. SYSTEM ADMINISTRATION │
│ ────────────────────────────── │
│ • User account management │
│ • Service monitoring and restart │
│ • Log rotation and cleanup │
│ • Backup automation │
│ • System health checks │
│ • Security auditing │
└────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────┐
│ 2. DEVOPS / SRE │
│ ──────────────── │
│ • Deployment scripts │
│ • Infrastructure provisioning (cloud-init, user-data) │
│ • Health checks and monitoring │
│ • Incident response automation │
│ • Runbook automation │
│ • Database backup/restore │
└────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────┐
│ 3. DATA PROCESSING │
│ ───────────────── │
│ • Log file parsing │
│ • CSV/JSON processing │
│ • Report generation │
│ • ETL pipelines │
│ • Metrics collection │
│ • Audit log analysis │
└────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────┐
│ 4. AUTOMATION │
│ ──────────── │
│ • Scheduled tasks (cron) │
│ • Batch processing │
│ • File organization │
│ • Build scripts │
│ • Test automation │
│ • Cleanup scripts │
└────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────┐
│ 5. CI/CD PIPELINES │
│ ───────────────── │
│ • Jenkinsfile stages │
│ • GitLab CI scripts │
│ • GitHub Actions │
│ • AWS CodeBuild buildspec │
│ • Azure DevOps scripts │
└────────────────────────────────────────────────────────────────────┘

By the end of this guide, you will be able to:

  1. ✅ Write robust bash scripts from scratch
  2. ✅ Handle complex text processing tasks
  3. ✅ Automate system administration tasks
  4. ✅ Debug and troubleshoot scripts
  5. ✅ Implement error handling and best practices
  6. ✅ Create production-ready automation
  7. ✅ Build CI/CD pipeline scripts
  8. ✅ Write Kubernetes helper scripts
  9. ✅ Create monitoring and alerting scripts
┌─────────────────────────────────────────────────────────────────────┐
│ LEARNING CURVE │
└─────────────────────────────────────────────────────────────────────┘
Level │
★★★★★ ┤ Advanced Topics
│ ╱ (Chapters 40+)
★★★★ ┤ ╱
│ ╱ Intermediate
★★★ ┤ ╱ (Chapters 20+)
│ ╱ ╱ Beginner
★★ ┤ ╱ ╱ (Chapters 1-10)
│ ╱ ╱ ╱
★ ┤ ╱ ╱ ╱
│ ╱ ╱ ╱
└────┴──┴──┴─────────────────────
Time/Study Effort

Before starting this guide, you should have:

  • Basic understanding of Linux command line
  • Familiarity with text editors (vim, nano)
  • Root or sudo access to a Linux system (for hands-on practice)
  • Basic understanding of system administration concepts

Example: Real-World Bash Script for DevOps

Section titled “Example: Real-World Bash Script for DevOps”

Here’s an example of what you’ll be able to create after completing this guide:

#!/usr/bin/env bash
#===============================================================================
# deploy.sh - Production Deployment Script
#
# This script demonstrates many concepts covered in this guide:
# - Strict mode
# - Functions
# - Error handling
# - Logging
# - Signal handling
# - Command line arguments
# - External tool integration
#===============================================================================
set -euo pipefail
# Configuration
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly APP_NAME="myapp"
readonly LOG_FILE="/var/log/${APP_NAME}/deploy.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
#===============================================================================
# Functions
#===============================================================================
log_info() {
echo -e "${GREEN}[INFO]${NC} $*" | tee -a "$LOG_FILE"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $*" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" | tee -a "$LOG_FILE" >&2
}
cleanup() {
local exit_code=$?
log_info "Cleaning up..."
# Add cleanup tasks here
exit $exit_code
}
check_dependencies() {
local deps=("docker" "kubectl" "helm")
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
log_error "Required dependency not found: $dep"
exit 1
fi
done
}
deploy() {
local environment="$1"
local version="$2"
log_info "Deploying $APP_NAME v$version to $environment"
# Pull Docker image
log_info "Pulling Docker image..."
docker pull "${APP_NAME}:${version}"
# Deploy with kubectl
log_info "Applying Kubernetes manifests..."
kubectl apply -f k8s/
# Wait for rollout
log_info "Waiting for deployment..."
kubectl rollout status deployment/"${APP_NAME}" -n "$environment"
# Verify
if kubectl get pods -n "$environment" | grep -q "Running"; then
log_info "Deployment successful!"
return 0
else
log_error "Deployment failed"
return 1
fi
}
#===============================================================================
# Main
#===============================================================================
trap cleanup EXIT
# Parse arguments
ENVIRONMENT="staging"
VERSION="latest"
while [[ $# -gt 0 ]]; do
case $1 in
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-v|--version)
VERSION="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [-e|--environment ENV] [-v|--version VERSION]"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Main execution
log_info "Starting deployment..."
check_dependencies
deploy "$ENVIRONMENT" "$VERSION"

Bash is an essential skill for any DevOps, SRE, or System Administrator. It provides:

  • Powerful scripting capabilities for automation
  • Direct system access for system administration
  • Industry standard for Linux/Unix environments
  • Foundation for learning other DevOps tools
  • Integration point for CI/CD pipelines

In the next chapter, we’ll set up your environment on Arch Linux and configure everything for optimal bash scripting.


  1. Check your current bash version
  2. List all available shells on your system
  3. Create your first “Hello World” script
  1. Explore bash built-in commands using enable -l
  2. Check your current shell options with set -o
  3. Look at bash completion scripts in /etc/bash_completion.d/
  1. Create an alias for common kubectl commands
  2. Write a script that checks if Docker is running
  3. Create a simple health check script

Next Chapter: Environment Setup on Arch Linux