Bash scripting is an essential skill for automating repetitive tasks on Linux. Whether you’re managing servers, running Docker containers, or just simplifying daily tasks, knowing how to write a Bash script can save you a ton of time.
In this guide, we’ll cover:
✅ What is Bash Scripting?
✅ Basic Bash Commands & Syntax
✅ How to Write Your First Script
✅ Using Variables & Loops
✅ Automating Tasks with Cron Jobs
If you’re looking to expand your Linux knowledge, check out these recommended Linux books:
📖 👉 Best Linux OS Books
Want to dive deeper into automation and scripting? Python is another powerful language for automation. Here are some great Python programming books:
📖 👉 Top Python Books
🎯 What is Bash Scripting?
Bash (Bourne Again SHell) is a command-line interpreter that lets you execute commands in Linux. A Bash script is simply a text file containing a sequence of Linux commands that can be executed automatically.
Why Use Bash Scripting?
✅ Automate routine tasks (backups, updates, etc.)
✅ Run multiple commands in sequence
✅ Schedule jobs using cron
✅ Manage system administration tasks
🛠 Step 1: Writing Your First Bash Script
1️⃣ Create a New Script File
Let’s create a simple script called hello.sh
:
nano hello.sh
2️⃣ Add the Shebang Line (#!
)
Every Bash script starts with a shebang (#!
), which tells the system to use Bash to execute the script.
#!/bin/bash
echo "Hello, World! This is my first Bash script."
3️⃣ Make the Script Executable
Before running the script, we need to give it execution permissions:
chmod +x hello.sh
4️⃣ Run the Script
Now, execute the script:
./hello.sh
You should see the output:
Hello, World! This is my first Bash script.
📝 Step 2: Using Variables in Bash
Variables allow us to store and reuse values in our scripts.
#!/bin/bash
name="Infotek User"
echo "Hello, $name! Welcome to Bash scripting."
Run the script:
./hello.sh
Output:
Hello, Infotek User! Welcome to Bash scripting.
Best Practices for Variables:
✅ Use lowercase for custom variables (e.g., username
)
✅ Use uppercase for environment variables (e.g., $HOME
, $PATH
)
🔄 Step 3: Using Loops in Bash
For Loop Example
#!/bin/bash
for i in {1..5}
do
echo "Iteration: $i"
done
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
While Loop Example
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
🎯 Step 4: Using Conditional Statements
If Statement Example
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
Run the script:
./script.sh
Example Output:
Enter a number: 12
The number is greater than 10.
📌 Step 5: Automating Tasks with Cron Jobs
We can schedule a Bash script to run automatically using cron jobs.
1️⃣ Create a Script for Automatic System Updates
#!/bin/bash
sudo apt update -y && sudo apt upgrade -y
echo "System updated successfully!"
2️⃣ Add it to Crontab
Open crontab:
crontab -e
Add the following line to run the script every day at 3 AM:
0 3 * * * /path/to/script.sh
✅ Now, your system will update automatically every day!
🔥 Bonus: A Real-World Example – Backup Script
Let’s create a backup script that compresses a directory and saves it with a timestamp.
#!/bin/bash
backup_dir="/home/user/Documents"
backup_dest="/home/user/backup"
timestamp=$(date +"%Y%m%d_%H%M%S")
backup_file="backup_$timestamp.tar.gz"
mkdir -p $backup_dest
tar -czf $backup_dest/$backup_file $backup_dir
echo "Backup saved as $backup_dest/$backup_file"
✅ Run this script manually or automate it using cron
!
🚀 Conclusion
Now, you’ve learned how to write Bash scripts, use variables, create loops, and automate tasks using cron
! 🎯
🔹 Next Steps:
- Try writing scripts for your daily tasks
- Automate server maintenance
- Explore more Bash functions like arrays, functions, and advanced scripting
💻 Looking for a Mini PC to Run Your Bash Scripts?
👉 Check Out AMD Mini PCs
📖 Want to Master Linux?
👉 Best Linux OS Books
🐍 Thinking About Learning Python for More Automation?
👉 Top Python Books
☁️ Prefer a Cloud Solution?
👉 Deploy on Vultr
🔹 Need More Tutorials? Comment Below! 🚀
Leave a Reply