Beginner's Guide to Shell Scripting Basics in Linux
Shell scripting is one of the most powerful skills you can learn in Linux. It allows you to automate repetitive tasks, build simple tools, and combine commands into workflows. Whether you’re an aspiring system administrator or DevOps engineer, shell scripting is a must-have skill.
What is a Shell Script?
A shell script is simply a text file that contains Linux commands executed in sequence by a shell (like bash, zsh, or sh).
Think of it as saving a series of terminal commands into a file so you can run them all at once.
Creating Your First Script
Create a new file:
nano hello.shAdd the following content:
#!/bin/bash echo "Hello, World!"
Save and exit.
Make it executable:
chmod +x hello.shRun it:
./hello.sh

Variables in Shell Scripts
Variables let you store and reuse values.
#!/bin/bash
name="Marwan"
echo "Hello, $name!"

Output:
Hello, Marwan!

Conditional Statements
Use if statements to add logic:
#!/bin/bash
if [ -f "/etc/passwd" ]; then
echo "File exists."
else
echo "File does not exist."
fi


Loops
Loops let you repeat tasks automatically.
For Loop Example:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Number: $i"
done


While Loop Example:
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count + 1))
done


Functions
Functions make scripts reusable and modular.
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "Linux User"


Useful Commands in Scripts
date→ Print current date and timeuptime→ Show system uptimedf -h→ Disk usagefree -m→ Memory usageps aux→ Running processes
These can all be combined into scripts for automation.
Hands-on Labs
Here are 9 labs to practice, sorted by difficulty.
🟢 Beginner Labs (Basic Scripting Skills)
Lab 1: Greeting Script
Ask for the user’s name (
read name) and print:Hello, <name>! Welcome to Linux.
✅ Goal: Learn input, variables, and echo.
Lab 2: Simple Calculator
- Write a script that takes two numbers as input and prints their sum.
Hint: Usereadand arithmetic$(( )).
✅ Goal: Practice input and basic math.
Lab 3: Even or Odd Checker
Ask for a number.
Print whether it’s even or odd.
Hint: Use modulus operator%.
✅ Goal: Practice conditionals.
🟡 Intermediate Labs (Automation & File Handling)
Lab 4: Backup a Directory
Takes a directory as input.
Creates a
.tar.gzbackup with today’s date.Saves it in
/tmp/backups/.
✅ Goal: Automate backups.
Lab 5: Log File Cleaner
- Find and delete all
.logfiles older than 7 days in/var/log/.
Hint: Usefind /var/log -name "*.log" -mtime +7 -delete.
✅ Goal: Automate system housekeeping.
Lab 6: User Creation Script
Ask for a username.
Create the user and set a default password.
Add them to the
developersgroup.
✅ Goal: Automate sysadmin tasks.
🔴 Advanced Labs (Reusable Tools & System Monitoring)
Lab 7: System Health Report
Print:
Current date & time (
date)Uptime (
uptime)Disk usage (
df -h)Memory usage (
free -m)Top 5 processes (
ps aux --sort=-%mem | head -n 6)
✅ Goal: Build a reusable admin tool.
Lab 8: Network Connectivity Checker
Read a list of hostnames from a file.
Loop through and
pingeach one.Print which hosts are up and which are down.
✅ Goal: Practice loops + conditionals with real networking.
Lab 9: Automated Service Monitor
Check if
nginxorapache2is running.If not, restart it and log the event to
/var/log/service_monitor.log.
✅ Goal: Automate monitoring & recovery.
Conclusion
With these labs, you’ll progress from simple echo scripts to building real-world automation tools. Shell scripting is a skill that grows with you: the more you practice, the more powerful your Linux workflows become.
Practice Pack Script
Here’s the linux-shell-labs.sh file (all 9 labs with comments):
#!/bin/bash
# Linux Shell Scripting Labs
# Run: chmod +x linux-shell-labs.sh && ./linux-shell-labs.sh
######################
# Beginner Labs
######################
# Lab 1: Greeting Script
greeting_lab() {
read -p "Enter your name: " name
echo "Hello, $name! Welcome to Linux."
}
# Lab 2: Simple Calculator
calculator_lab() {
read -p "Enter first number: " a
read -p "Enter second number: " b
echo "Sum: $((a + b))"
}
# Lab 3: Even or Odd Checker
evenodd_lab() {
read -p "Enter a number: " num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even."
else
echo "$num is odd."
fi
}
######################
# Intermediate Labs
######################
# Lab 4: Backup a Directory
backup_lab() {
read -p "Enter directory to backup: " dir
mkdir -p /tmp/backups
tar -czvf "/tmp/backups/backup-$(date +%Y%m%d).tar.gz" "$dir"
echo "Backup created in /tmp/backups/"
}
# Lab 5: Log File Cleaner
logclean_lab() {
echo "Cleaning log files older than 7 days..."
find /var/log -name "*.log" -mtime +7 -exec rm {} \;
echo "Old log files cleaned."
}
# Lab 6: User Creation Script
usercreate_lab() {
read -p "Enter username: " user
sudo useradd -m "$user"
echo "$user:password123" | sudo chpasswd
sudo usermod -aG developers "$user"
echo "User $user created and added to developers group."
}
######################
# Advanced Labs
######################
# Lab 7: System Health Report
sysreport_lab() {
echo "=== System Health Report ==="
echo "Date: $(date)"
echo "Uptime: $(uptime)"
echo ""
echo "=== Disk Usage ==="
df -h
echo ""
echo "=== Memory Usage ==="
free -m
echo ""
echo "=== Top 5 Processes ==="
ps aux --sort=-%mem | head -n 6
}
# Lab 8: Network Connectivity Checker
netcheck_lab() {
hosts=("google.com" "github.com" "localhost")
for host in "${hosts[@]}"; do
if ping -c 1 "$host" &> /dev/null; then
echo "$host is UP"
else
echo "$host is DOWN"
fi
done
}
# Lab 9: Automated Service Monitor
servicemon_lab() {
service="nginx"
if ! systemctl is-active --quiet "$service"; then
echo "$service is not running. Restarting..."
sudo systemctl restart "$service"
echo "$(date): Restarted $service" >> /var/log/service_monitor.log
else
echo "$service is running."
fi
}
######################
# Menu
######################
while true; do
echo -e "\nChoose a Lab to Run:
1) Greeting
2) Calculator
3) Even/Odd
4) Backup Directory
5) Log Cleaner
6) User Creation
7) System Report
8) Network Check
9) Service Monitor
0) Exit"
read -p "Enter choice: " choice
case $choice in
1) greeting_lab ;;
2) calculator_lab ;;
3) evenodd_lab ;;
4) backup_lab ;;
5) logclean_lab ;;
6) usercreate_lab ;;
7) sysreport_lab ;;
8) netcheck_lab ;;
9) servicemon_lab ;;
0) exit ;;
*) echo "Invalid choice" ;;
esac
done