Getting Started with Shell Scripting for DevOps: A Beginner's Dive

WasiWasi
7 min read

Introduction

As a third-year Computer Science and Engineering student, I recently began diving deeper into the world of DevOps. In this exciting journey, one of the most fundamental and powerful skills I’ve picked up is Shell Scripting.

Before you dive into advanced DevOps tools like Jenkins, Docker, or Kubernetes, it’s important to master the basics. And shell scripting is one of those essential building blocks that makes everything else smoother and faster. It’s not just about writing lines of code — it's about automating workflows, saving time, and improving reliability in your development and deployment processes.

In this blog, I’ll walk you through what I’ve learned about shell scripting, how it fits into the DevOps ecosystem, and how you, as a student or beginner, can start leveraging it today.


What is Shell Scripting?

A shell script is a file containing a series of commands that are executed by the shell — typically the Bash shell in most Linux environments.

It’s like giving your computer a set of instructions that it follows step by step.

For example, instead of manually running 5 commands every time you want to start your development server, you could put them all in a .sh file and run it with a single command.

This is powerful — especially in the world of DevOps, where automation is everything.

Here’s a simple script:

bashCopyEdit#!/bin/bash
echo "Hello from DevOps Shell Script!"

Save this to a file called hello.sh, then run:

bashCopyEditchmod +x hello.sh
./hello.sh

Just like that, your first script runs successfully.


Why Shell Scripting is Essential in DevOps

If you’ve read about or worked on any DevOps process, you already know that automation is the core of everything — from software building and testing to deployment and monitoring.

Shell scripts help automate:

  • Setting up environments

  • Pulling and deploying code from Git

  • Creating backups and cleanup tasks

  • Monitoring systems

  • Handling CI/CD operations

  • Configuring and provisioning infrastructure

When combined with cron jobs, Dockerfiles, or cloud automation pipelines, shell scripts are incredibly powerful.


My Learning Curve as a Student

When I first started exploring DevOps, I jumped directly into tools like Docker and GitHub Actions. But soon I realized that a lot of tutorials and real-world projects heavily relied on shell scripts to tie everything together.

At that point, I decided to go back to the basics and really understand how Bash scripting works.

Through this process, I:

  • Learned how to use the Linux terminal more effectively

  • Created automation scripts for file management and backups

  • Integrated shell scripts into a CI/CD workflow for a Node.js project

  • Understood how DevOps engineers build custom solutions with minimal dependencies

This hands-on knowledge was more valuable than any theoretical reading.

1. Shebang – Defining the Shell Interpreter

Every script should start with a shebang to tell the system which shell to use.

bashCopyEdit#!/bin/bash
# Let the Bash games begin.

This ensures your script runs in the right environment — Bash in this case — avoiding unexpected behavior.


2. Variables – Storing and Referencing Data

You can assign values to variables and reuse them throughout the script.

bashCopyEditname="Wasi"
echo "Welcome, $name! The script has been expecting you..."

Variables help avoid repetition and make scripts easier to update and scale.


3. Conditionals – Making Decisions

Use if statements to handle different scenarios based on logic.

bashCopyEditif [ "$name" == "Wasi" ]; then
  echo "Access granted. Deploy responsibly."
else
  echo "Access denied. We do not want a tworld war."
fi

Conditionals are essential for dynamic behavior — reacting to user input, environment values, or execution results.


4. Loops – Repeating Tasks

Loops help execute a block of code multiple times without writing it repeatedly.

bashCopyEditfor i in {1..5}
do
  echo "Running task $i... Please don’t Ctrl+C me again."
done

Whether you’re processing files, iterating over servers, or running repetitive cleanup tasks, loops are a core scripting tool.


5. Functions – Reusing Code

Functions allow you to define blocks of code that can be reused with different inputs.

bashCopyEditgreet() {
  echo "Summoning $1... prepare the DevOps rituals."
}

greet "Wasi"

Functions promote modularity and make your scripts easier to test and maintain.


6. User Input – Taking Commands from the Human Overlords

You can make your scripts interactive by accepting input from the user.

bashCopyEditread -p "What is your name, O terminal whisperer? " user
echo "Nice to meet you, $user. Let’s automate something chaotic."

User input is useful for flexible scripts that require runtime decisions or configuration.

7. Exit Status – Knowing If Things Went Boom 💥

Every shell command returns an exit status. 0 means success; anything else means something went wrong.

bashCopyEditls /etc/passwd
echo "Status: $?"  # 0 means the universe is still intact.

ls /not/a/real/path
echo "Status: $?"  # Non-zero. Something just exploded in the matrix.

Checking exit codes is critical for building reliable automation. It helps you decide what happens next if something fails.


8. Comments – Talking to Future You

Use comments to annotate what your script is doing. It’s a habit your future self (and your teammates) will thank you for.

bashCopyEdit# This loop backs up files before Wasi starts experimenting again.
for file in *.conf; do
  cp "$file" "$file.bak"
done

Comments improve readability and act as lightweight documentation within the code.


9. Command Substitution – Putting Output Inside Variables

You can store the result of a command into a variable using $(...).

bashCopyEditcurrent_time=$(date)
echo "The shell script gods have awakened at: $current_time"

This is handy when you need dynamic values like timestamps, usernames, or outputs from other scripts.


10. File Testing – Interrogating the Filesystem

You can use conditionals to check file properties before performing actions.

bashCopyEditif [ -f "./deploy.sh" ]; then
  echo "deploy.sh exists. Time to launch the starship."
else
  echo "deploy.sh not found. Abort mission, Captain."
fi

Testing for files, directories, permissions, etc., helps avoid runtime errors and unintended consequences.

Real-Life DevOps Use Cases Where I Used Shell Scripting

As part of my DevOps minor project, I built automation tools using shell scripts. Here are some of the real-world use cases where they proved invaluable:

Automating Project Setup

bashCopyEdit#!/bin/bash
echo "Setting up project..."
git clone https://github.com/myrepo/project.git
cd project
npm install

Running a Deployment Script

bashCopyEdit#!/bin/bash
echo "Deploying to production..."
git pull origin main
npm run build
pm2 restart all

Backup Script

bashCopyEdit#!/bin/bash
backup_name="backup_$(date +%F_%T).tar.gz"
tar -czf $backup_name /home/ubuntu/app
echo "Backup created: $backup_name"

Log Cleanup

bashCopyEdit#!/bin/bash
echo "Deleting logs older than 7 days..."
find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;

These examples might seem basic, but they help eliminate repetitive work and reduce the chance of human error.


How Shell Scripting Integrates with DevOps Tools

Shell scripting integrates beautifully with tools like:

  • Git: Automating commits, merges, or hooks

  • Jenkins: Custom build steps using shell commands

  • Docker: Writing entrypoint scripts or custom container startup routines

  • AWS CLI: Automating cloud resource provisioning

  • CI/CD Pipelines: Running deployment steps

In one of my projects, I used shell scripting to automate pulling the latest code, installing dependencies, restarting the app using PM2, and logging the status.

The entire deployment process was simplified into a single executable script.


Best Practices I Followed

As I experimented with writing longer scripts, I came across some practices that made my scripts cleaner, safer, and more maintainable:

  1. Always start with #!/bin/bash

  2. Add comments generously

  3. Use set -e to stop the script when any command fails

  4. Use functions for repeated logic

  5. Always test scripts in a safe environment

  6. Handle edge cases and user input carefully


Helpful Resources I Used as a Student

Here are the tutorials and documentation I found extremely useful:

  • LinuxCommand.org

  • The Bash Guide for Beginners

  • Bash Scripting Cheatsheet by Devhints

  • YouTube channels like NetworkChuck, TechWorld with Nana, and The Linux Guy

  • Practice on WSL, Ubuntu VM, or GitHub Codespaces


Final Thoughts

If you’re a student trying to learn DevOps, do not skip shell scripting.

Start small, automate your own project tasks, and gradually integrate it into tools like Git, Docker, or Jenkins. You don’t need to be a Bash wizard — you just need to be able to read, write, and debug scripts that make your life easier.

Learning shell scripting has helped me:

  • Understand Linux systems more deeply

  • Speed up my workflows

  • Build confidence when using DevOps tools

  • Create reliable scripts for deployment and automation

23
Subscribe to my newsletter

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

Written by

Wasi
Wasi

I am a passionate full-stack developer with expertise in the MERN stack, Next.js, and PostgreSQL. My experience spans building scalable web applications, and I specialize in creating seamless user experiences and robust back-end systems. I’m deeply interested in AI and machine learning, and have worked on several projects that leverage these technologies to solve real-world challenges. Continuously exploring new trends, I am committed to enhancing my skill set to stay ahead in the tech world. Whether working solo or within teams, I focus on delivering impactful solutions and driving projects forward with dedication and creativity. Dive into my life on GitHub :https://github.com/reallywasi