Introduction to Bash Scripting in Red Hat Linux

Welcome to Day 18 of the 30 Days of Linux Challenge!
Today’s milestone is a major step toward real-world automation and system control: Bash scripting.
Whether you’re managing servers, running tasks, or automating builds — Bash scripting is the backbone of Linux efficiency.
📚 Table of Contents
Why Bash Scripting Matters
Bash scripting allows you to:
- Automate repetitive manual work
- Create scheduled maintenance jobs
- Wrap complex Linux commands into one script
- Respond to system conditions dynamically
Instead of typing 5 commands every day — you can run one script and be done.
Your First Bash Script
Create the script
nano myfirstscript.sh
Add:
echo “Hello, world!”
date
whoami
Make it executable:
chmod +x myfirstscript.sh
./myfirstscript.sh
Working with Variables
myname=”CloudWhistler”
echo “Welcome, $myname!”
Notes:
- No spaces around =
- Use $variable to access
User Input in Scripts
echo “What is your name?”
read username
echo “Hello, $username!”
Conditional Logic (if-else)
read -p “Enter a number: ” num
if [ $num -gt 10 ]; then
echo “That’s a big number!”
else
echo “That’s a small number.”
fi
Common operators:
Operator Meaning
-eq Equal
-ne Not equal
-gt Greater than
-lt Less than
-ge Greater or equal
-le Less or equal
Loops: For and While
For Loop
for i in {1..5}
do
echo “Step $i”
done
While Loop
counter=1
while [ $counter -le 5 ]
do
echo “Count: $counter”
((counter++))
done
Functions in Bash
greet() {
echo “Hello, $1!”
}
greet “Linux”
greet “World”
Functions allow you to organize larger scripts into manageable blocks.
Try It Yourself
🧪 Practice ideas:
Ask the user for their name and greet them.
Create a loop that counts from 1 to 10.
Write a function that prints disk usage (df -h).
Combine conditionals + variables + user input.
Real-World Bash Script Examples
Task | Script Purpose |
---|---|
Backup /etc directory |
Automate backups and schedule with cron |
Restart service if crashed | Monitor with logic and restart commands |
Rotate logs | Archive and clean logs regularly |
Check disk usage + email alert | Prevent system crashes |
Rename/move bulk files | Save hours of manual renaming |
Why This Matters
Bash scripting is the gateway to serious automation in Linux.
✅ Speeds up your work
✅ Makes tasks repeatable and error-free
✅ Builds a foundation for tools like Ansible, Docker, Jenkins, and CI/CD pipelines