Bash — like a real teacher sitting next to you with a cup of chai, explaining everything step-by-step in very simple English with lots of examples.
Imagine your computer (especially Linux, macOS, or Windows with WSL/WSL2 or Git Bash) has two ways to talk to it:
- Clicking things (mouse + icons + windows) → this is called GUI (Graphical User Interface)
- Typing commands → this is called CLI (Command Line Interface) or Terminal
Bash is the most popular teacher/language that understands the commands you type in the terminal.
What does “Bash” actually stand for?
Bourne Again SHell (It’s a pun — improved version of the old “Bourne Shell” which was called sh)
So Bash = modern, powerful version of the old Unix command-line language.
Two Main Things Bash Does
- Interactive mode → You type one command → Bash runs it immediately (what most people do every day)
- Script mode → You write many commands in a file → Bash reads and runs all of them one by one automatically (this is called Bash scripting)
Most people say “Bash Tutorial” when they actually mean Bash Scripting Tutorial — how to write small programs (scripts) using Bash.
Let’s learn both parts properly!
Part 1: Using Bash Interactively (Daily Commands)
Open your terminal (on Ubuntu → Ctrl + Alt + T, on macOS → Terminal app)
Try these one by one:
|
0 1 2 3 4 5 6 7 |
# Shows current date and time date |
|
0 1 2 3 4 5 6 7 |
# Shows who you are logged in as whoami |
|
0 1 2 3 4 5 6 7 |
# Shows your current folder location pwd |
|
0 1 2 3 4 5 6 7 |
# Lists files and folders in current location ls |
|
0 1 2 3 4 5 6 7 |
# Better version — shows more details + colors ls -la |
|
0 1 2 3 4 5 6 7 |
# Create a new empty folder mkdir my_tutorial |
|
0 1 2 3 4 5 6 7 |
# Go inside that folder cd my_tutorial |
|
0 1 2 3 4 5 6 7 |
# Come back one folder up cd .. |
|
0 1 2 3 4 5 6 7 |
# See what is inside /etc folder (system settings) ls /etc |
These are the baby steps — Bash is just listening and running these small commands.
Part 2: Bash Scripting — Writing Mini Programs
Now the fun part begins!
A Bash script is just a normal text file that contains many commands — and you can run the whole file with one command.
Let’s create our first script.
Step 1: Create file
|
0 1 2 3 4 5 6 |
nano hello.sh |
(or use any editor: gedit hello.sh, code hello.sh, vim hello.sh)
Step 2: Write this (very important — copy exactly)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/bash # This is a comment — computer ignores it echo "Hello friend!" echo "Welcome to Bash class" echo "Today is: $(date)" echo "You are: $(whoami)" echo "Your current folder is: $(pwd)" |
Step 3: Save and exit (Nano: Ctrl+O → Enter → Ctrl+X)
Step 4: Give permission to run it (very important!)
|
0 1 2 3 4 5 6 |
chmod +x hello.sh |
Step 5: Run it!
|
0 1 2 3 4 5 6 |
./hello.sh |
You should see something like:
|
0 1 2 3 4 5 6 7 8 9 10 |
Hello friend! Welcome to Bash class Today is: Wed Feb 25 12:45:23 IST 2026 You are: webliance Your current folder is: /home/webliance |
Congratulations! You just wrote and ran your first Bash script! 🎉
Very Important Concepts (Explained like to a friend)
- Shebang line must be first#!/bin/bash → tells computer: “Please use Bash to run this file”
- echo → prints message on screen
- $( ) → runs command and puts result inside
Example:
|
0 1 2 3 4 5 6 |
echo "Your home folder is: $HOME" |
Variables (super useful)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/bin/bash name="Rahul" age=24 city="Hyderabad" echo "Hi, I am $name" echo "I am $age years old" echo "I live in $city" |
Run it → output:
|
0 1 2 3 4 5 6 7 8 |
Hi, I am Rahul I am 24 years old I live in Hyderabad |
Taking Input from User (read command)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash echo "What is your name?" read username echo "Hello $username, nice to meet you!" echo "How old are you?" read userage echo "You are $userage — wow!" |
If-else condition (decision making)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash echo "Enter your age:" read age if [ $age -ge 18 ]; then echo "You can vote! 🗳️" else echo "You are too young to vote 😔" fi |
For Loop (repeat something)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash for i in 1 2 3 4 5 do echo "Counting: $i" done |
Better version (1 to 10):
|
0 1 2 3 4 5 6 7 8 9 |
for i in {1..10} do echo "Number $i" done |
While Loop (repeat until something)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/bin/bash count=1 while [ $count -le 5 ] do echo "Loop $count" ((count++)) done |
Practical Real-life Script Example
Backup your important folder automatically:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash # backup-script.sh SOURCE="/home/webliance/Documents" BACKUP="/home/webliance/Backups" DATE=$(date +%Y-%m-%d) echo "Starting backup on $DATE..." tar -czf "$BACKUP/documents-$DATE.tar.gz" "$SOURCE" if [ $? -eq 0 ]; then echo "Backup successful! 🎉" else echo "Backup failed! 😢" fi |
Run daily → never lose files!
Quick Summary Table (most used things)
| What you want to do | Command/Example |
|---|---|
| Print something | echo “Hello” |
| Store value | name=”Sara” |
| Use variable | echo $name or echo “$name” |
| Get input | read city |
| Compare numbers | [ $a -gt $b ] |
| Compare strings | [ “$str” == “yes” ] |
| Run command and use result | today=$(date) |
| If condition | if [ condition ]; then … fi |
| Loop 1 to 10 | for i in {1..10}; do … done |
Want to go deeper? Tell me which topic you want next:
- Functions in Bash
- Arguments to scripts ($1, $2…)
- Case statement
- Working with files (find, grep, sed, awk)
- Error handling
- Cron jobs (run script automatically every day)
Just say — “explain Bash functions with examples” or anything!
Happy Bash learning! 🐧 Keep practicing — typing is the only way to become fast. 😊
