Shell Scripting Basics in Linux: Automate Tasks and Boost Productivity
Shell scripting is one of the most powerful tools in a Linux user’s arsenal. With just a few lines of Bash code, you can automate repetitive tasks, streamline your workflow, and manage your system like a pro. Whether you want to automate daily backups, clean up temporary files, or schedule system checks, shell scripting is the key to working smarter—not harder.
In this guide, we’ll cover the foundational concepts of Bash scripting, including variables, conditionals, loops, and how to pass arguments. By the end, you’ll be ready to write your own scripts and schedule them with cron
.
What Is Shell Scripting?
A shell script is a plain-text file containing a series of commands that the shell (typically Bash) can execute in order. It behaves just like typing commands into the terminal—but faster, more consistently, and often hands-free.
Shell scripting is ideal for:
-
Automating repetitive tasks
-
Performing system maintenance
-
Managing file systems and backups
-
Running scheduled jobs via
cron
🛠️ Getting Started: Your First Bash Script
🔹 Create a Script File:
nano backup.sh
🔹 Add the Shebang:
#!/bin/bash
This tells the system to use the Bash shell to run the script.
🔹 Sample Script:
#!/bin/bash
echo "Starting backup..."
cp -r ~/Documents ~/Backups
echo "Backup completed!"
🔹 Make It Executable:
chmod +x backup.sh
🔹 Run It:
./backup.sh
🧮 Using Variables in Shell Scripts
Variables make scripts dynamic and reusable.
#!/bin/bash
name="Alice"
echo "Hello, $name!"
You can also use variables to store paths, file names, and user input.
🔁 Loops and Conditionals
🔹 If Statements:
#!/bin/bash
if [ -f “file.txt” ]; then
echo “file.txt exists.”
else
echo “file.txt not found.”
fi
🔄 For Loops:
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
done
🔁 While Loops:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count is $count"
((count++))
done
These constructs allow your scripts to make decisions and repeat tasks efficiently.
📥 Passing Arguments to Scripts
Scripts can accept user input through positional parameters.
#!/bin/bash
echo "Hello, $1!"
Run it with:
./greet.sh Alice
🔹 Access More Arguments:
-
$1
,$2
, … = positional parameters -
$#
= number of arguments -
$@
= all arguments as a list
⚠️ Basic Error Handling
It’s important to handle errors gracefully to avoid crashes or data loss.
🔹 Check Exit Status:
cp file1.txt /backup/
if [ $? -ne 0 ]; then
echo "Backup failed!"
exit 1
fi
🔹 Exit Early on Errors:
set -e # Exit script if any command fails
Add this at the beginning of critical scripts to ensure they stop on failure.
📆 Automate with cron
cron
lets you schedule scripts to run automatically.
🔹 Edit Crontab:
crontab -e
🔹 Example Cron Job:
0 2 * * * /home/user/backup.sh
This runs the script every day at 2:00 AM. Be sure the script is executable and paths are absolute.
Real-World Use Cases
✅ 1. Automate Backups
#!/bin/bash
tar -czf ~/Backups/home-$(date +%F).tar.gz ~/Documents
✅ 2. Clean Up Temp Files
#!/bin/bash
find /tmp -type f -mtime +3 -exec rm {} \;
✅ 3. Rotate Logs
#!/bin/bash
mv /var/log/app.log /var/log/app.log.bak
touch /var/log/app.log
These examples save time and reduce the risk of manual errors.
What’s Next?
Now that you’ve mastered the basics of Bash scripting—variables, conditionals, loops, arguments, and automation—it’s time to level up.
In the next post, we’ll explore:
-
Advanced scripting techniques (functions, arrays, traps)
-
Other popular shells like Zsh and Fish
-
Creating reusable tools with modular scripts