Chapter 50: Scripting
Scripting — that is, Bash Scripting.
Imagine this:
- Typing one command → like asking your friend to pass you one glass of water
- Writing a script → like writing a small note: “Hey friend, please do these 10 things every morning: boil water, make chai, add sugar, pour in cup, bring it to my table, clean the stove, put the cup in sink, wipe table, close gas, and say ‘chai ready boss’.”
You give that note once → your friend does all 10 steps automatically every time you say “run the note”.
That’s exactly what Bash scripting is:
Writing many commands in a normal text file → giving it a name → telling Bash “please run everything written in this file one by one automatically”.
What is a Bash Script? (Super Simple Definition)
A Bash script is just a plain text file that contains:
- A special first line called shebang → tells the computer “use Bash to run this file”
- A list of Bash commands (the same ones you type in terminal)
- Comments (lines starting with #) so you remember why you wrote what
And once you make it executable (chmod +x), you can run the whole thing with one short command — like magic!
Why Learn Scripting in 2026? (Real Reasons from Hyderabad Life)
- Automate boring/repetitive tasks (backup files every night, rename 500 photos, install software daily)
- Deploy websites/servers faster (git pull + restart nginx + clear cache in one click)
- Make your own tools (a personal “chai reminder” script, file organizer, system health checker)
- Work on cloud servers / DevOps / automation (AWS, GitHub Actions, Docker, CI/CD all use Bash scripts)
- Look super professional when you say “I wrote a script to do this”
Step-by-Step – Your First Real Script (Let’s Do It Together!)
Step 1: Create the File
|
0 1 2 3 4 5 6 7 |
nano hello.sh # or gedit hello.sh, code hello.sh, vim hello.sh — any editor |
Step 2: Write This (Copy Exactly – First Line is Magic!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash # This is a comment — computer ignores lines starting with # echo "=====================================" echo " Namaste from Hyderabad! 🚩 " echo " Welcome to Bash Scripting Class! " echo "=====================================" echo "" echo "Today is: $(date)" echo "You are logged in as: $(whoami)" echo "Your current folder is: $(pwd)" echo "Your home folder is: $HOME" echo "" echo "Script finished successfully! 🎉" echo "Keep practicing — you're doing great!" |
Step 3: Save & Exit
Nano: Ctrl+O → Enter → Ctrl+X
Step 4: Make It Executable (Super Important!)
|
0 1 2 3 4 5 6 7 8 |
chmod +x hello.sh # or chmod 755 hello.sh |
Without this step → you get “Permission denied” when you try to run it.
Step 5: Run Your Script!
Two ways (both work):
|
0 1 2 3 4 5 6 7 8 |
./hello.sh # or full path bash hello.sh |
You should see beautiful output like:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
===================================== Namaste from Hyderabad! 🚩 Welcome to Bash Scripting Class! ===================================== Today is: Wed Feb 26 09:45:12 IST 2026 You are logged in as: webliance Your current folder is: /home/webliance/bash_class Your home folder is: /home/webliance Script finished successfully! 🎉 Keep practicing — you're doing great! |
Congratulations! 🎉 You just wrote and ran your first Bash script!
What Makes It a Script? (Key Parts Explained)
- Shebang line (must be first line, no space before #!)
|
0 1 2 3 4 5 6 7 |
#!/bin/bash # or sometimes #!/usr/bin/env bash (more portable) |
Tells system: “Please use Bash interpreter to run this file”
- Comments (#)
|
0 1 2 3 4 5 6 |
# This explains what the script does |
Ignored by computer — but super important for future-you!
- Commands — same as terminal
|
0 1 2 3 4 5 6 |
echo, date, whoami, pwd, ls, mkdir, cp, rm, tar, etc. |
- Special tricks inside scripts
|
0 1 2 3 4 5 6 7 8 |
$(command) → run command and insert result $HOME → your home path $USER → your username |
Next Level – Make It Ask Your Name (Interactive Script)
Edit hello.sh again (nano hello.sh) and replace content with 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 |
#!/bin/bash echo "What's your good name, boss?" read your_name echo "" echo "Arre waah, $your_name ji! Very nice to meet you 😄" echo "From Hyderabad, right?" echo "How old are you?" read age if [ "$age" -ge 18 ]; then echo "You are $age — adult level unlocked! 🗳️" else echo "You are $age — keep learning, champ! 🚀" fi echo "Script finished — see you next time!" |
Run it again:
|
0 1 2 3 4 5 6 |
./hello.sh |
Now it talks to you — this is interactive scripting!
Summary Table – Scripting Basics at a Glance
| Part | What it is | Example / Rule |
|---|---|---|
| File name | Usually ends with .sh | hello.sh, backup.sh, deploy.sh |
| First line | Shebang | #!/bin/bash |
| Make executable | chmod +x filename.sh | Required to run with ./filename |
| Run it | ./filename.sh or bash filename.sh | ./ is current directory |
| Comments | Lines starting with # | # This is ignored |
| Variables | name=”value” then echo “$name” | Always quote “$var” |
| Run command in echo | echo “Today: $(date)” | $( ) = command substitution |
| Input from user | read variable_name | Waits for you to type |
Your 7-Day Scripting Challenge (Start Today!)
Day 1 → Write & run the first hello.sh above Day 2 → Add read for name & age (like second example) Day 3 → Make a script that creates backup of ~/Documents (cp -r or tar) Day 4 → Add if-else: check if folder exists (test -d) Day 5 → Script that shows disk usage (df -h) + memory (free -h) Day 6 → Colorful output (echo -e “\e[32mSuccess!\e[0m”) Day 7 → Put it in ~/bin/ so you can run from anywhere
Got the big idea now? Scripting = turning many manual commands into one automatic superpower tool.
Any part confusing? Want next: “Teacher, explain variables in detail” or “if-else conditions” or “loops (for/while)” or “how to pass arguments to script” or “make script run every day automatically (cron)”?
Just say — teacher is ready in Hyderabad! Keep writing small scripts every day — soon you’ll feel like a Bash wizard! 🐧✨📜 😄
