Bash Scripting Basics Guide — Automate Tasks with Shell Scripts

Bash scripting basics guide for developers: learn to write shell scripts that automate repetitive tasks, process files in bulk, orchestrate deployments, and chain commands into powerful workflows. Covers variables, loops, conditionals, and functions with real examples.

What Is Bash Scripting?

Bash (Bourne Again Shell) is both a command-line interpreter and a scripting language. A bash script is a text file containing a sequence of shell commands that run as a program. Bash scripting basics are essential for any developer working on Linux/macOS — they let you automate deployments, process log files, manage backups, and create development utilities without learning a full programming language.

How to Write Your First Bash Script

  1. Create a file: touch script.sh
  2. Add the shebang line: First line must be #!/bin/bash
  3. Write commands: Add the commands you want to execute.
  4. Make executable: chmod +x script.sh
  5. Run it: ./script.sh or bash script.sh

Key Bash Scripting Concepts

Variables

#!/bin/bash
NAME="DevKits"
echo "Hello, $NAME"

# Command substitution
TODAY=$(date +%Y-%m-%d)
echo "Today is $TODAY"

# Special variables
echo "Script name: $0"
echo "First argument: $1"
echo "All arguments: $@"
echo "Exit code: $?"

Conditionals

if [ -f "file.txt" ]; then
    echo "File exists"
elif [ -d "directory" ]; then
    echo "Directory exists"
else
    echo "Neither found"
fi

# Numeric comparison
if [ $COUNT -gt 10 ]; then
    echo "Count is greater than 10"
fi

Loops

# For loop
for file in *.log; do
    echo "Processing $file"
done

# While loop
COUNT=0
while [ $COUNT -lt 5 ]; do
    echo "Count: $COUNT"
    COUNT=$((COUNT + 1))
done

# Loop over array
SERVERS=("web1" "web2" "db1")
for server in "${SERVERS[@]}"; do
    echo "Deploying to $server"
done

Functions

deploy() {
    local ENV=$1
    echo "Deploying to $ENV..."
    # deployment commands here
}

deploy "production"
deploy "staging"

Use Cases

Automated Backups

A simple bash script can compress and timestamp a directory, upload it to S3, and delete backups older than 30 days — all scheduled via cron. Three hours of scripting replaces months of manual work.

Deployment Automation

Most CI/CD pipelines use bash scripts at their core: pull latest code, run tests, build Docker image, push to registry, trigger rolling deployment. Understanding bash scripting basics lets you write and debug these pipelines effectively.

→ Try DevKits Developer Tools Free
aiforeverthing.com — Developer tools. No signup required.

Frequently Asked Questions

What is the shebang line in bash scripts?

The shebang (#!/bin/bash or #!/usr/bin/env bash) on the first line tells the OS which interpreter to use. #!/usr/bin/env bash is more portable as it finds bash wherever it's installed on the system.

How do I handle errors in bash scripts?

Add set -e at the top to exit immediately if any command fails, set -u to error on undefined variables, and set -o pipefail to catch errors in pipelines. Together: set -euo pipefail is the recommended default for robust scripts.

How do I read user input in a bash script?

Use the read command: read -p "Enter your name: " NAME. The -p flag displays a prompt. Use -s for silent input (passwords): read -sp "Password: " PASS.

What is the difference between single quotes and double quotes in bash?

Double quotes allow variable expansion: "$NAME" evaluates to the variable's value. Single quotes treat everything literally: '$NAME' is the literal string $NAME. Use double quotes when you want variables to expand, single quotes to pass literal strings.

How do I schedule a bash script with cron?

Edit your crontab with crontab -e and add a line like 0 2 * * * /path/to/script.sh to run at 2 AM daily. The format is minute hour day month weekday command. Use crontab.guru to generate and verify cron expressions.

Recommended Hosting for Developers

  • Hostinger — From $2.99/mo. Excellent for static sites and Node.js apps.
  • DigitalOcean — $200 free credit for new accounts. Best for scalable backends.
  • Namecheap — Budget-friendly shared hosting with free domain.