What Is Bash and Why Does It Matter?

Bash (Bourne Again SHell) is the default command-line shell on most Linux distributions and macOS. It's been around since 1989 and remains one of the most widely used scripting environments in the world — especially in server administration, DevOps, and CI/CD pipelines.

Learning Bash means you can automate nearly anything on a Unix-like system without installing additional software. Your scripts run wherever Bash is installed, which is essentially everywhere.

The Anatomy of a Bash Script

Every Bash script starts with a shebang line that tells the OS which interpreter to use:

#!/bin/bash
# This is a comment
echo "Hello, World!"

Save this as hello.sh, make it executable with chmod +x hello.sh, and run it with ./hello.sh.

Variables and User Input

Variables in Bash are simple — no type declarations needed:

#!/bin/bash
NAME="DescaScript"
echo "Welcome to $NAME"

# Read user input
read -p "Enter your name: " USER_NAME
echo "Hello, $USER_NAME!"

Key rule: no spaces around the = sign when assigning variables. This trips up most beginners.

Conditionals and Comparisons

#!/bin/bash
FILE="config.yaml"

if [ -f "$FILE" ]; then
    echo "Config file found."
else
    echo "Config file missing — creating default."
    touch "$FILE"
fi

Common test operators:

  • -f — file exists and is a regular file
  • -d — directory exists
  • -z — string is empty
  • -n — string is not empty
  • -eq, -ne, -lt, -gt — numeric comparisons

Loops: For and While

# For loop over a list
for COLOR in red green blue; do
    echo "Color: $COLOR"
done

# While loop with a counter
COUNT=1
while [ $COUNT -le 5 ]; do
    echo "Iteration $COUNT"
    ((COUNT++))
done

Functions in Bash

Functions keep your scripts DRY (Don't Repeat Yourself):

#!/bin/bash

greet() {
    local name="$1"  # $1 is the first argument
    echo "Hello, $name!"
}

greet "Developer"
greet "World"

Use local for variables inside functions to avoid accidentally overwriting global variables — a common source of hard-to-find bugs.

A Real-World Example: System Health Check Script

#!/bin/bash

echo "=== System Health Check ==="
echo "Date: $(date)"
echo ""

echo "-- Disk Usage --"
df -h / | tail -1

echo ""
echo "-- Memory --"
free -h | grep Mem

echo ""
echo "-- CPU Load --"
uptime

echo ""
echo "-- Top 5 Processes by Memory --"
ps aux --sort=-%mem | head -6

Save this as health_check.sh, make it executable, and schedule it with cron to run daily. It's a genuinely useful script from day one.

Essential Bash Safety Tips

  • Add set -e at the top of scripts to exit immediately on any error.
  • Add set -u to treat unset variables as errors — catches typos in variable names.
  • Always quote your variables: "$VAR" not $VAR, especially when paths may contain spaces.
  • Test scripts on non-critical data before running them on production systems.
  • Use shellcheck yourscript.sh to catch common mistakes before running.

Bash rewards methodical learners. Build your scripts incrementally, test each piece, and you'll have a powerful automation toolkit running in no time.