Linux9 min read·

Bash Scripting Tutorial: Automate Linux Tasks with Shell Scripts

Learn bash scripting from scratch. Write scripts to automate system administration, file processing, and security tasks with real examples.

What Are Bash Scripts?

Bash scripts are text files containing a sequence of commands. The shell reads and executes them in order — automating repetitive tasks that would take hours manually. If you run the same three commands every morning, write a bash script instead.

Your First Script

Create a file called hello.sh:

#!/bin/bash
echo "Hello, $(whoami)! Today is $(date +%A)."

Make it executable and run it:

chmod +x hello.sh
./hello.sh

The #!/bin/bash line (shebang) tells the system which interpreter to use.

Variables and Input

#!/bin/bash
NAME="Security Analyst"
read -p "Enter your target: " TARGET
echo "Scanning $TARGET for $NAME work..."

Variables need no type declaration. Use $ to reference them. The read command captures user input.

Conditionals

#!/bin/bash
PORT=22

if [ $PORT -eq 22 ]; then
    echo "SSH is open"
elif [ $PORT -eq 80 ]; then
    echo "HTTP is open"
else
    echo "Unknown service on port $PORT"
fi

Common test operators:

  • -eq, -ne, -gt, -lt — numeric comparison
  • =, != — string comparison
  • -f, -d — file/directory exists
  • -z, -n — string empty/not empty

Loops

For Loop:

#!/bin/bash
for ip in 192.168.1.{1..254}; do
    ping -c 1 -W 1 $ip &>/dev/null && echo "$ip is alive"
done

While Loop:

#!/bin/bash
COUNT=1
while [ $COUNT -le 5 ]; do
    echo "Attempt $COUNT"
    ((COUNT++))
done

Functions

#!/bin/bash
check_port() {
    local host=$1
    local port=$2
    timeout 1 bash -c "echo >/dev/tcp/$host/$port" 2>/dev/null
    return $?
}

if check_port 192.168.1.1 22; then
    echo "Port 22 is open"
fi

Functions make scripts modular and reusable. Use local for function variables.

Practical Example: Backup Script

#!/bin/bash
SOURCE="/var/www/html"
DEST="/backups/$(date +%Y%m%d_%H%M%S).tar.gz"
LOG="/var/log/backup.log"

echo "$(date): Starting backup of $SOURCE" >> $LOG
tar -czf "$DEST" "$SOURCE" 2>> $LOG

if [ $? -eq 0 ]; then
    echo "$(date): Backup successful: $DEST" >> $LOG
else
    echo "$(date): Backup FAILED" >> $LOG
fi

Practical Example: Log Analyzer

#!/bin/bash
LOGFILE=$1
echo "=== Failed SSH Attempts ==="
grep "Failed password" "$LOGFILE" | awk '{print $11}' | sort | uniq -c | sort -rn | head -10

Run with: ./analyze.sh /var/log/auth.log

Practical Example: Automated Scanner

#!/bin/bash
TARGET=$1
echo "Scanning $TARGET..."
nmap -sV -oN "scan_$(date +%s).txt" "$TARGET"
echo "Scan complete. Results saved."

Tips for Writing Scripts

  1. Always include the shebang line
  2. Quote variables to handle spaces: "$VAR"
  3. Use set -e to exit on errors
  4. Comment non-obvious logic
  5. Test with echo before executing real commands
  6. Use shellcheck to catch common mistakes

Bash scripting is a force multiplier for Linux administrators and security professionals. Start automating your daily tasks today.

Ready to practice?

Apply what you learned with free hands-on labs on XpertClass. Deploy real Docker sandboxes — no setup required.