Bash Scripting for Beginners

Tue, June 3, 2025 - 1 min read

Bash scripting is a must-have skill for Linux administrators. It lets you automate repetitive tasks and streamline system management.

Your First Script

Create a file called hello.sh:

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

Make it executable and run it:

chmod +x hello.sh
./hello.sh

Variables

name="Linux"
echo "Hello, $name!"
 
# Command substitution
files=$(ls /home)
echo "Files: $files"

Conditionals

if [ -f "/etc/passwd" ]; then
  echo "File exists"
else
  echo "File not found"
fi

Loops

# For loop
for i in {1..5}; do
  echo "Iteration $i"
done
 
# While loop
count=1
while [ $count -le 5 ]; do
  echo "Count: $count"
  ((count++))
done

Functions

backup_file() {
  cp "$1" "$1.bak"
  echo "Backed up $1"
}
 
backup_file "/etc/nginx/nginx.conf"

Bash scripting turns repetitive manual tasks into reliable, automated workflows. Practice by automating small tasks first.