Beginner's Guide to Shell Scripting Basics in Linux
Learn the Essentials of Shell Scripting in Linux for Beginners
In a world where technology advances at a breakneck pace, I'm a passionate developer always inspired by the latest innovations. The revolutionary science fiction of The Matrix (1999) ignited my love for the genre and the idea of what's possible beyond the screen. When I'm not immersed in code, you can find me lost in the melodies of a piano, a peaceful counterpoint to the logic and structure of development. It’s in this harmony of technology and art that I find my true rhythm.
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 == 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 -czf /tmp/backups/backup-$(date +%F).tar.gz "$dir"
echo "Backup saved in /tmp/backups/"
}
# Lab 5: Log File Cleaner
logclean_lab() {
find /var/log -name "*.log" -mtime +7 -exec rm -f {} \;
echo "Old log files cleaned."
}
# Lab 6: User Creation Script
usercreate_lab() {
read -p "Enter username: " user
sudo useradd -m "$user"
echo "$user:password" | 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 - $(date)"
uptime
df -h
free -m
ps aux --sort=-%mem | head -n 6
}
# Lab 8: Network Connectivity Checker
netcheck_lab() {
read -p "Enter file with hostnames: " file
while read host; do
if ping -c 1 -W 1 "$host" &> /dev/null; then
echo "$host is UP"
else
echo "$host is DOWN"
fi
done < "$file"
}
# Lab 9: Automated Service Monitor
servicemon_lab() {
service="nginx"
if ! systemctl is-active --quiet $service; then
echo "$(date): $service was down, restarting..." >> /var/log/service_monitor.log
sudo systemctl restart $service
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
