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)

  1. Save time — don’t type the same 10–15 commands every day
  2. Avoid mistakes — once it works, it always works the same way
  3. Automate boring tasks — backup files, rename 1000 photos, clean logs, deploy website
  4. Share with friends/team — send one file, they run it
  5. Run automatically — every night at 2 AM, every time computer starts, etc.
  6. Make your own tools — “chai-reminder.sh”, “project-cleaner.sh”, “server-health-check.sh”

The 5 Golden Rules Every Bash Script Must Follow

  1. First line must be the shebang (magic line that says “use Bash”)

    Bash

    Must be very first line, no space before #

  2. Save file with .sh extension (helps you recognize it — not compulsory but good habit)

    Examples: backup.sh, greet.sh, clean.sh

  3. Make it executable (very important — otherwise “Permission denied”)

    Bash

  4. Run it with ./ (current directory) or full path

    Bash

    (or bash myscript.sh — but ./ is shorter & cooler)

  5. Use # for comments so future-you understands

    Bash

Let’s Write & Run Your First Real Script Right Now

Step 1: Create the file

Bash

Step 2: Copy-paste exactly this:

Bash

Step 3: Save & exit (nano → Ctrl+O → Enter → Ctrl+X)

Step 4: Make executable

Bash

Step 5: Run it!

Bash

You should see something beautiful like:

text

Congratulations! 🎉 You just created, saved, made executable, and ran your first real Bash script!

What Makes This a Script? (Key Parts Explained)

Part Why it’s important Example from our script
Shebang #!/bin/bash Tells system: use Bash to interpret this file First line – must be exact
Comments with # Explains what script does (for future you) # Namaste from Hyderabad!
echo Prints messages (your script can talk!) echo “Hello boss!”
$(command) Runs command and inserts result $(date ‘+%A, %d %B %Y %I:%M %p’)
$VARIABLE Uses built-in or custom variables $USER, $HOME
Pipes Sends output of one command to another

Bonus – Make It Even Better (Small Upgrade)

Edit the file again and add this at the end:

Bash

Now run ./first_script.sh again — it asks you!

Quick Summary Table – Bash Script at a Glance

Question Answer / Rule
What is it? Text file full of Bash commands
File extension Usually .sh (helps you recognize)
First line #!/bin/bash (shebang – mandatory)
Make runnable chmod +x filename.sh
How to run ./filename.sh or bash filename.sh
Comments Start with #
Print message echo “text”
Use variable VARor” VAR or ” {VAR}”
Run command inside string “$(command)”
Ask user input read variable_name

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! 🇮🇳🐧🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *