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.
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.
pwd
👉 Example output:
/home/username/Documents
3️⃣ cd — Change Directory
- Moves you to another folder.
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.
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!
rm file.txt # Delete file
rm -r foldername # Delete folder and its contents
8️⃣ cp — Copy Files and Folders
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
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
echo "What is your name?"
read name
echo "Hello, $name!"
1️⃣4️⃣ if / else / fi — Basic Conditional
if [ $name == "Alice" ]; then
echo "Hi Alice!"
else
echo "You're not Alice!"
fi
1️⃣5️⃣ for / while loops — Repeat Tasks
Example #1
for i in 1 2 3
do
echo "Number $i"
done
Example #2
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:
#!/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:
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!