Part 1: Introduction to Bash
What is Bash?
Bash (short for Bourne Again Shell) is a command-line interpreter and scripting language commonly found in Linux and macOS. It allows users to interact with the system, run commands, automate tasks, and write reusable scripts.
When you open the Terminal on Linux or macOS, you’re typically using Bash by default (unless your system uses Zsh or Fish).
Think of Bash as the “brain of the terminal” — it reads your commands, executes them, and displays the output.
💡 Why Learn Bash Scripting?
Here’s why every developer or DevOps engineer should master Bash:
| Benefit | Description |
|---|---|
| Automation | Automate repetitive tasks like backups, updates, deployments, etc. |
| Efficiency | Save time by chaining commands and running batch processes. |
| System Management | Manage users, services, and configurations directly from scripts. |
| Integration | Bash integrates easily with other languages (Python, Node.js, PHP, etc.). |
| DevOps Essential | Used in CI/CD pipelines, server provisioning, and Docker scripts. |
In short: Bash scripting makes you more productive and gives you superpowers in Linux-based environments. ⚙️
🧩 Understanding How Bash Works
When you type a command in the terminal (like ls or cd), the following happens:
- Bash reads your command.
- It interprets it — recognizing arguments, options, and operators.
- It executes the command using the system kernel.
- Finally, it outputs the result back to you.
Example:
ls -l /homeThis command:
- Lists all files in
/home - Displays details (
-lmeans long format) - Outputs file size, permissions, and modification time
⚙️ Setting Up Your Bash Environment
Before writing scripts, make sure Bash is available on your system.
🖥️ For Linux:
Bash is usually pre-installed.
Check with:
bash --version🍏 For macOS:
Also comes pre-installed, but you can update via Homebrew:
brew install bash🪟 For Windows (Optional):
You can use:
- Windows Subsystem for Linux (WSL)
- Git Bash (lightweight, perfect for beginners)
Once installed, open your terminal and type:
echo "Hello, Bash!"
✅ You’re ready to start scripting!
🧰 Writing Your First Bash Script
Let’s write a simple script named hello.sh.
Step 1: Create the Script File
touch hello.shStep 2: Open and Edit the File
Use any text editor (e.g., nano, vim, or VS Code):
nano hello.shStep 3: Add the Script Content
#!/bin/bash
# This is a simple Bash script
echo "Hello, World! Welcome to Bash scripting!"Step 4: Make It Executable
chmod +x hello.sh
Step 5: Run It
./hello.sh🟢 Output:
Hello, World! Welcome to Bash scripting!Congratulations! You’ve just written your first Bash script.
What Is the #! (Shebang) Line?
The first line of every Bash script should be:
#!/bin/bashIt tells the system to execute the script using Bash — not another shell (like sh or zsh).
Without it, your script may not behave as expected on other systems.
💬 Common Commands You Should Know
| Command | Description |
|---|---|
pwd | Print the current directory |
ls | List files and directories |
cd | Change directory |
mkdir | Create a new directory |
rm | Remove files/directories |
cp | Copy files |
mv | Move or rename files |
echo | Print text or variables |
cat | Display file contents |
Pro Tip: Run Commands in One Line
You can chain commands using semicolons ;:
mkdir test; cd test; echo "Script test successful!" > result.txtSummary
In this first part, you’ve learned:
- What Bash is and why it matters
- How to check your Bash version and set up the environment
- How to write and execute your first script
- Basic Bash commands to navigate and interact with files
Up next:
👉 Part 2: Bash Basics — Variables, Inputs, Outputs, and Exit Codes
