🚀 Day 5 of My DevOps Journey: Bash Scripting (Beginner to Advanced)

Ritesh SinghRitesh Singh
3 min read

Today was all about automation. I dove deep into Bash scripting, one of the most fundamental skills in any DevOps engineer’s toolkit. From creating simple scripts to understanding logic, loops, and functions — I now feel confident writing scripts to automate tasks on Linux systems.


🧠 What is Bash Scripting?

Bash scripting lets you write a sequence of Linux commands in a file and execute them all at once. It's used to:

  • Automate repetitive tasks

  • Create backups, alerts, monitoring tools

  • Configure and manage environments

Scripts are saved as .sh files and run using the Bash shell.


✅ Step 1: My First Bash Script

I created and ran my first script:

touch hello.sh
chmod +x hello.sh

Inside hello.sh:

#!/bin/bash
echo "Hello, DevOps World!"

Then executed it:

./hello.sh

This printed: Hello, DevOps World!

The line #!/bin/bash is a shebang that tells Linux to use the Bash interpreter.


✅ Step 2: Variables in Bash

#!/bin/bash
name="ritesh"
echo "Welcome, $name!"

🧠 Bash variables are declared without spaces and accessed using $.


✅ Step 3: Accepting User Input

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"

The read command takes user input and stores it in a variable.


✅ Step 4: Conditions with If/Else

#!/bin/bash
echo "Enter a number:"
read num

if [ "$num" -gt 10 ]; then
  echo "Greater than 10"
else
  echo "10 or less"
fi

Here, -gt means “greater than”. We also use square brackets [] to evaluate conditions.


✅ Step 5: Using Loops

➕ For Loop

for i in 1 2 3 4 5
do
  echo "Count: $i"
done

🔁 While Loop

i=1
while [ $i -le 5 ]
do
  echo "Step $i"
  ((i++))
done

🧠 Loops help execute code repeatedly until a condition is met.


✅ Step 6: Functions in Bash

greet() {
  echo "Hi $1, welcome to scripting!"
}
greet ritesh

Functions help keep code modular and reusable. $1 accesses the first argument passed.


✅ Step 7: Arguments and Script Parameters

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"

Run this using:

./myscript.sh devops

🧠 $0 is the script name, $1, $2, etc., are the passed arguments.


✅ Step 8: Error Handling with Exit Codes

mkdir /tmp/myfolder || {
  echo "Failed to create folder"
  exit 1
}

This is a way to stop the script if something fails. || means “if the command fails, do the following.”


🔧 Mini Project Ideas (Next Practice)

ScriptWhat It Does
user-check.shChecks if a Linux user exists
disk-check.shWarns if disk usage > 80%
backup.shArchives and backs up a folder

📘 Key Bash Commands Summary

ConceptExample
Shebang#!/bin/bash
Variablename="ritesh"
User Inputread name
If Conditionif [ "$x" -gt 5 ]; then ...
For Loopfor i in 1 2 3
While Loopwhile [ $i -le 5 ]
Functiongreet() { ... }
Script Arg$1, $2
Exit Codeexit 1

✨ Reflection

“Today, I moved from typing commands manually to scripting them like a DevOps engineer. Bash scripting feels like giving Linux superpowers.”

With this knowledge, I'm ready to write automation scripts and build small tools for my own Linux system.


🔗 GitHub Journal: https://github.com/ritesh355/devops-journal
🔗
Connect: https://linkedin.com/in/ritesh-singh-092b84340

#devops #linux #bash #shellscripting #automation #selftaught #100DaysOfDevOps #learninginpublic

0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

Hi, I’m Ritesh 👋 I’m on a mission to become a DevOps Engineer — and I’m learning in public every single day.With a full-time commitment of 8–10 hours daily, I’m building skills in: ✅ Linux✅ Git & GitHub✅ Docker & Kubernetes✅ AWS EC2, S3✅ Jenkins, GitHub Actions✅ Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Let’s connect, collaborate, and grow together 🚀 #100DaysOfDevOps #LearningInPublic #DevOps