Chapter 52: Bash Script
Bash script
What is a Bash Script? (The Honest, Simple Answer)
A Bash script is nothing more than a normal text file that contains a list of Bash commands — the same commands you already know how to type in the terminal (ls, cd, echo, mkdir, cp, tar, grep, curl, etc.).
But instead of typing them one by one every time, you write them once inside the file, save it, make it executable, and then you can run all of them automatically with one single short command.
It’s like:
- Typing commands manually → like cooking one roti at a time
- Writing a Bash script → writing a small recipe: “mix atta, add water, knead, roll, cook on tawa, flip, serve hot” → give this recipe to someone → they make perfect rotis every time without thinking.
Why Do People Make Scripts? (Real Reasons)
- Save time — don’t type the same 10–15 commands every day
- Avoid mistakes — once it works, it always works the same way
- Automate boring tasks — backup files, rename 1000 photos, clean logs, deploy website
- Share with friends/team — send one file, they run it
- Run automatically — every night at 2 AM, every time computer starts, etc.
- Make your own tools — “chai-reminder.sh”, “project-cleaner.sh”, “server-health-check.sh”
The 5 Golden Rules Every Bash Script Must Follow
-
First line must be the shebang (magic line that says “use Bash”)
Bash0123456<span class="line">#!/bin/bash</span>Must be very first line, no space before #
-
Save file with .sh extension (helps you recognize it — not compulsory but good habit)
Examples: backup.sh, greet.sh, clean.sh
-
Make it executable (very important — otherwise “Permission denied”)
Bash0123456<span class="line">chmod +x myscript.sh</span> -
Run it with ./ (current directory) or full path
Bash0123456<span class="line">./myscript.sh</span>(or bash myscript.sh — but ./ is shorter & cooler)
-
Use # for comments so future-you understands
Bash0123456<span class="line"># This line is ignored by computer</span>
Let’s Write & Run Your First Real Script Right Now
Step 1: Create the file
|
0 1 2 3 4 5 6 7 |
<span class="line">nano first_script.sh</span> <span class="line"># or gedit first_script.sh, code first_script.sh, vim first_script.sh</span> |
Step 2: Copy-paste exactly this:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span class="line">#!/bin/bash</span> <span class="line"># ==============================================</span> <span class="line"># My first Bash script – Namaste from Hyderabad!</span> <span class="line"># Author: Webliance</span> <span class="line"># Date: February 2026</span> <span class="line"># ==============================================</span> <span class="line">echo "======================================="</span> <span class="line">echo " Hello boss! Chai time? ☕ "</span> <span class="line">echo " Welcome to Bash Scripting Class! "</span> <span class="line">echo "======================================="</span> <span class="line">echo ""</span> <span class="line">echo "Today's date & time : $(date '+%A, %d %B %Y %I:%M %p')"</span> <span class="line">echo "You are logged in as: $USER"</span> <span class="line">echo "Current folder : $(pwd)"</span> <span class="line">echo "Your home folder : $HOME"</span> <span class="line">echo ""</span> <span class="line">echo "Quick system check:"</span> <span class="line">df -h | grep "/dev/sda" || echo "Main disk info not found"</span> <span class="line">free -h | head -2</span> <span class="line">echo ""</span> <span class="line">echo "Script completed successfully! 🎉"</span> <span class="line">echo "Keep practicing — you're doing awesome!"</span> |
Step 3: Save & exit (nano → Ctrl+O → Enter → Ctrl+X)
Step 4: Make executable
|
0 1 2 3 4 5 6 |
<span class="line">chmod +x first_script.sh</span> |
Step 5: Run it!
|
0 1 2 3 4 5 6 |
<span class="line">./first_script.sh</span> |
You should see something beautiful like:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="line">=======================================</span> <span class="line"> Hello boss! Chai time? ☕ </span> <span class="line"> Welcome to Bash Scripting Class! </span> <span class="line">=======================================</span> <span class="line">Today's date & time : Wednesday, 26 February 2026 11:15 AM</span> <span class="line">You are logged in as: webliance</span> <span class="line">Current folder : /home/webliance/bash_learning</span> <span class="line">Your home folder : /home/webliance</span> <span class="line">Quick system check:</span> <span class="line">/dev/sda1 100G 45G 50G 48% /</span> <span class="line"> total used free shared buff/cache available</span> <span class="line">Mem: 15Gi 2.3Gi 9.8Gi 0.4Gi 3.1Gi 12Gi</span> <span class="line">Swap: 2.0Gi 0B 2.0Gi</span> <span class="line">Script completed successfully! 🎉</span> <span class="line">Keep practicing — you're doing awesome!</span> |
Congratulations! 🎉 You just created, saved, made executable, and ran your first real Bash script!
What Makes This a Script? (Key Parts Explained)
Bonus – Make It Even Better (Small Upgrade)
Edit the file again and add this at the end:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="line">echo ""</span> <span class="line">echo "Want to backup your Documents folder now? (y/n)"</span> <span class="line">read answer</span> <span class="line">if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then</span> <span class="line"> backup_file="docs_backup_$(date +%Y-%m-%d).tar.gz"</span> <span class="line"> tar -czf "$backup_file" ~/Documents/</span> <span class="line"> echo "Backup created: $backup_file 🎯"</span> <span class="line">else</span> <span class="line"> echo "No backup today — maybe tomorrow! 😄"</span> <span class="line">fi</span> |
Now run ./first_script.sh again — it asks you!
Quick Summary Table – Bash Script at a Glance
Got the big picture now? A Bash script is your personal robot assistant — write once, run forever, automate anything.
Next class — what do you want?
- “Teacher, explain variables & how to use them properly”
- “Show me if-else conditions with examples”
- “How to make loops (for, while)”
- “Pass arguments to script ($1, $2…)”
- “Make script run every day automatically (cron)”
Just say the topic — teacher is ready! Keep writing small scripts every day — even 5 lines — you’ll become fast very soon. All the best from Hyderabad! 🇮🇳🐧🚀
