Shell Scripting for Beginners: A Detailed Guide
Have you ever wondered how developers automate repetitive tasks or configure systems with just a few lines of code? That magic is often done with shell scripts! If you're new to the world of programming or just starting with Linux/Unix, this blog is the perfect place to begin your journey into shell scripting.
🌟 What is Shell Scripting?
Shell scripting is writing a series of commands for the shell (the command-line interpreter) to execute. These scripts are saved in a file (usually with a .sh extension) and can automate tasks like file backups, system monitoring, software installation, and more.
Think of it like this:
💬 You give instructions to your computer in a list, and the computer executes them one by one — that’s shell scripting.
💡 What is a Shell?
A shell is a program that takes commands from the user and gives them to the operating system to perform. The most common shell is Bash (Bourne Again SHell), used in most Linux distributions and macOS.
Other popular shells include:
- sh – Bourne shell
- zsh – Z shell
- ksh – Korn shell
- fish – Friendly Interactive SHell
But for most beginners, Bash is the go-to shell.
🧾 Why Learn Shell Scripting?
- ✅ Automate repetitive tasks
- ✅ Save time and reduce human error
- ✅ Manage servers and environments
- ✅ Build deployment scripts for projects
- ✅ It’s a foundational skill for DevOps, Linux admins, and backend developers
📁 Basic Shell Script Structure
Here’s what a simple shell script looks like:
#!/bin/bash
# This is a comment
echo "Hello, world!"
🔍 Explanation:
#!/bin/bash: This is called a shebang. It tells the system to use the Bash shell to interpret this script.# This is a comment: Anything after#is a comment and not executed.echo "Hello, world!": This prints text to the screen.
📌 How to Create and Run a Shell Script
Step 1: Create a file
nano hello.sh
Paste this:
#!/bin/bash
echo "Welcome to Shell Scripting!"
Step 2: Save and exit (in nano: press Ctrl + X, then Y, then Enter)
Step 3: Give the script permission to execute
chmod +x hello.sh
Step 4: Run the script
./hello.sh
And you'll see:
Welcome to Shell Scripting!
🛠️ Variables in Shell Scripts
Variables store data like numbers or strings.
#!/bin/bash
name="John"
echo "Hello, $name!"
Output:
Hello, John!
🔑 Note:
- No spaces around
= - Use
$variable_nameto use the value
🧮 Arithmetic Operations
Shell supports simple arithmetic using expr or $(()):
a=10
b=5
# Using expr
result=$(expr $a + $b)
echo "Sum is $result"
# Using double parentheses
echo "Product is $(($a * $b))"
🔁 Conditional Statements
If-else
#!/bin/bash
num=10
if [ $num -gt 5 ]; then
echo "$num is greater than 5"
else
echo "$num is not greater than 5"
fi
Operators:
-eq: equal to-ne: not equal-gt: greater than-lt: less than-ge: greater or equal-le: less or equal
🔄 Loops in Shell Scripts
For Loop
for i in 1 2 3 4 5
do
echo "Number: $i"
done
While Loop
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count + 1))
done
📥 Reading User Input
echo "What is your name?"
read username
echo "Hello, $username!"
📂 Working with Files
Check if file exists:
filename="myfile.txt"
if [ -f $filename ]; then
echo "$filename exists"
else
echo "$filename does not exist"
fi
Loop through files in a folder:
for file in *.txt
do
echo "Processing $file"
done
🔧 Functions in Shell Scripts
greet() {
echo "Hello, $1!"
}
greet "Alice"
Output:
Hello, Alice!
💣 Error Handling (Exit Status)
Every command returns an exit status:
0means success- Anything else means an error
You can check it using $?:
mkdir newfolder
if [ $? -eq 0 ]; then
echo "Directory created"
else
echo "Failed to create directory"
fi
📋 Example: Backup Script
Let’s create a real-world use case — backing up a folder.
#!/bin/bash
# Simple backup script
backup_folder="/home/user/documents"
destination="/home/user/backup"
date=$(date +%F)
backup_file="backup-$date.tar.gz"
tar -czf $destination/$backup_file $backup_folder
echo "Backup completed: $backup_file"
📚 Tips for Beginners
- Always test scripts with
bash script.shbefore making them executable - Use comments to explain your logic
- Use shellcheck (a linting tool) to catch script errors
- Learn by automating your own small tasks
📘 Final Thoughts
Shell scripting might look simple, but it's incredibly powerful. Whether you're managing servers, automating tasks, or just learning Linux, shell scripts can save you hours of work and make you a more efficient developer.
✨ Start small, experiment, and keep scripting — soon, you'll be automating like a pro!
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.