DEV Community

1suleyman
1suleyman

Posted on

🐚 What Is Bash Scripting? (And Why It’s a Superpower in Your Terminal)

Hey everyone πŸ‘‹

If you're diving into Linux, DevOps, or automation, you've probably heard people mention Bash scripting. I used to think it was something only hardcore sysadmins used. But as I got deeper into the terminal, I realized Bash scripting is like having a personal assistant that can automate almost anything you do repeatedly.

Let me break it down the way I wish someone had for me early on πŸ‘‡


🧠 Think of It Like a Recipe for Your Terminal

Let’s say you make coffee every morning. You go to the kitchen, boil water, add coffee, stir, and pour.

Now imagine writing that routine down step-by-step, so anyone (or anything) could do it exactly the same way β€” every single time.

That’s what Bash scripting does β€” but for your computer. It takes your terminal commands and turns them into repeatable, automated recipes.

Whether you're moving files, installing packages, or deploying apps, Bash scripting is like giving your terminal a to-do list.


βš™οΈ Why Use Bash Scripting?

βœ… 1. Automate Repetitive Tasks

Need to check server health? Backup a folder? Run a multi-step build process?

Instead of typing the same commands every time, write them once in a .sh script β€” and run it with one command. It's like saving your favorite playlist.

πŸ› οΈ 2. Access Everything the Terminal Can

Every command you type manually can be put in a script β€” file moves, installs, pings, you name it. It’s a gateway into the full power of your system.

πŸ” 3. Add Logic, Loops, and Input

Bash isn’t just about lists of commands. You can:

  • Use if/else statements to make decisions
  • Loop through files or numbers
  • Ask for user input with read
  • Pass arguments from the command line

That turns Bash from a script into a mini-program.


πŸ§ƒ The Basics: How Bash Scripting Works

Every script starts like this:

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

That line tells your system to run the script using the Bash interpreter.

To make your script executable:

chmod +x script.sh
Enter fullscreen mode Exit fullscreen mode

Then you can run it:

./script.sh
Enter fullscreen mode Exit fullscreen mode

You can even put your scripts in ~/bin and add it to your PATH to run them from anywhere.


πŸ”€ Variables, Conditionals, Loops β€” Oh My!

πŸ”Έ Variables

greeting="Hello"
echo $greeting
Enter fullscreen mode Exit fullscreen mode

No spaces around the =! And to use the variable, just add $.


πŸ”Έ Conditionals

if [ $x -lt 5 ]; then
  echo "Less than 5"
else
  echo "5 or more"
fi
Enter fullscreen mode Exit fullscreen mode

Use -eq, -ne, -lt, -gt, etc. for number comparisons.

For strings:

if [ "$a" == "$b" ]; then
  echo "Same!"
fi
Enter fullscreen mode Exit fullscreen mode

Always quote your variables to avoid errors.


πŸ”Έ Loops

For loop:

for word in $sentence; do
  echo $word
done
Enter fullscreen mode Exit fullscreen mode

While loop:

while [ $x -lt 3 ]; do
  echo $x
  x=$((x + 1))
done
Enter fullscreen mode Exit fullscreen mode

Until loop:

until [ $x -eq 3 ]; do
  echo $x
  x=$((x + 1))
done
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ Getting Input

From the user:

echo "What's your name?"
read name
echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode

From command-line arguments:

echo "First color is $1"
Enter fullscreen mode Exit fullscreen mode

To handle many:

for arg in "$@"; do
  echo $arg
done
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Aliases: Shortcuts for Your Scripts

Let’s say you wrote a script called greet.sh. You can alias it:

alias greet='./greet.sh'
Enter fullscreen mode Exit fullscreen mode

Want it to always run with an argument?

alias greet3='./greet.sh 3'
Enter fullscreen mode Exit fullscreen mode

Drop that in your ~/.bashrc or ~/.bash_profile, and now greet3 works like a built-in command.


🧩 Final Thoughts

Bash scripting might look basic at first, but it’s a hidden powerhouse. It helps you:

  • Automate your workflow
  • Reduce human error
  • Make your terminal smarter

If you’re just starting with Linux or DevOps, learning Bash is one of the best investments you can make.

I’m still building up my own Bash toolkit β€” so if you’ve got cool tips, shortcuts, or questions, hit me up on LinkedIn or leave a comment here. Let’s make the terminal feel a little more human-friendly πŸ§πŸ’¬

Top comments (0)