Bash Shell Scripting — The Most Common Commands You Need to Know

Updated: July 20, 2025, 09:28 PM IST

Here's a detailed and easy-to-follow blog article on Bash shell scripting — with a focus on commonly used commands for beginners. I've written it in a friendly, clear style, with examples where useful.

What You’ll Learn in This Article

🐚 What is Bash? What is a Shell?

Why Learn Bash Scripting?

🔑 Most Common Bash Commands You Should Know

1️⃣ echo — Print Text or Variables

2️⃣ pwd — Show Current Directory

3️⃣ cd — Change Director

4️⃣ ls — List Files and Folders

5️⃣ touch — Create a New Empty File

6️⃣ mkdir — Make a New Directory

7️⃣ rm — Remove Files or Folders

8️⃣ cp — Copy Files and Folders

9️⃣ mv — Move or Rename Files and Folders

1️⃣0️⃣ cat — Show File Contents

1️⃣1️⃣ grep — Search Inside Files

1️⃣2️⃣ chmod — Change File Permissions

1️⃣3️⃣ read — Get Input from the User

1️⃣4️⃣ if / else / fi — Basic Conditional

1️⃣5️⃣ for / while loops — Repeat Tasks

Let’s go through the essential commands — the ones you’ll use again and again!

1️⃣ echo — Print Text or Variables

This command shows messages on the screen.

bash
Copy Copied!
echo "Hello, world!"

👉 Output:

Hello, world!

  • You can also print variables:

name="Alice"
echo "Hello, $name"

👉 Output:

Hello, Alice

2️⃣ pwd — Show Current Directory

Shows where you are in the file system.

bash
Copy Copied!
pwd

👉 Example output:

/home/username/Documents

3️⃣ cd — Change Directory

  • Moves you to another folder.

bash
Copy Copied!
cd /path/to/folder
cd ~  # Go to your home directory
cd ..   # Go up one directory

4️⃣ ls — List Files and Folders

  • Lists the contents of a directory.
bash
Copy Copied!
ls  # Basic list
ls -l   # Long format with permissions
ls -a  # Show hidden files

ls -la

5️⃣ touch — Create a New Empty File

touch myfile.txt

6️⃣ mkdir — Make a New Directory

mkdir new_folder

7️⃣ rm — Remove Files or Folders

⚠️ Be careful — rm deletes without sending to trash!

bash
Copy Copied!
rm file.txt   # Delete file
rm -r foldername  # Delete folder and its contents

8️⃣ cp — Copy Files and Folders

bash
Copy Copied!
cp file1.txt file2.txt     # Copy file1.txt to file2.txt
cp -r folder1 folder2  # Copy folder1 to folder2

9️⃣ mv — Move or Rename Files and Folders

bash
Copy Copied!
mv oldname.txt newname.txt   # Rename
mv file.txt /path/to/folder/       # Move file

1️⃣0️⃣ cat — Show File Contents

cat myfile.txt

1️⃣1️⃣ grep — Search Inside Files

Find text in files.

grep "hello" myfile.txt

👉 Shows lines with the word "hello".

1️⃣2️⃣ chmod — Change File Permissions

Make a script file executable.

chmod +x myscript.sh

1️⃣3️⃣ read — Get Input from the User

bash
Copy Copied!
echo "What is your name?"
read name
echo "Hello, $name!"

1️⃣4️⃣ if / else / fi — Basic Conditional

bash
Copy Copied!
if [ $name == "Alice" ]; then
 echo "Hi Alice!"
else
 echo "You're not Alice!"
fi

1️⃣5️⃣ for / while loops — Repeat Tasks

Example #1

bash
Copy Copied!
for i in 1 2 3
do
 echo "Number $i"
done

Example #2

bash
Copy Copied!
count=1
while [ $count -le 3 ]
do
 echo "Count $count"
 count=$((count + 1))
done

📝 Simple Example Script Using These Commands

Let’s put it all together:

bash
Copy Copied!
#!/bin/bash

echo "Enter a filename:"
read filename

if [ -f $filename ]; then
 echo "File exists. Contents:"
 cat $filename
else
 echo "File does not exist. Creating file..."
 touch $filename
 echo "New file created: $filename"
fi

👉 What this does:

  • Asks for a file name
  • Checks if it exists
  • Shows its contents or creates a new empty file

How to Start Practicing Bash Scripting

Step 1: Open a terminal (Linux, macOS, or WSL/Git Bash on Windows)
Step 2: Use a simple text editor like nano, vim, or VS Code
Step 3: Save your script as myscript.sh
Step 4: Make it executable:

bash
Copy Copied!
chmod +x myscript.sh

Step 5: Run it:

./myscript.sh

💡 Tips for Learning

✨ Start small — practice each command
✨ Combine commands into simple scripts
✨ Always test scripts on test files/folders first (avoid losing data!)
✨ Read other people’s scripts to learn tricks

🌟 Conclusion

Bash scripting is powerful, flexible, and easy to start with. By learning these common commands, you’ll be able to:

  • Work faster in the terminal
  • Automate boring tasks
  • Build useful tools

🔑 Next step: Try writing your own scripts using these commands. Start with small tasks, and grow from there!