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)