The Power of Shell Scripting in DevOps

M ChidrupM Chidrup
2 min read

What is Shell Scripting?

Shell scripting is writing a series of commands for the Unix/Linux shell to automate repetitive tasks. Think of it as a lightweight, quick-to-execute programming language tailor-made for system-level automation.

Shells commonly used:

  • bash (Bourne Again SHell)

  • sh (Bourne Shell)

  • zsh, ksh, and more…


⚙️ Why Shell Scripting Matters in DevOps

In a DevOps workflow, automation is key. Here’s where shell scripts become indispensable:

  • CI/CD Automation: Automate build, test, and deploy processes.

  • Server Setup & Configuration: Install packages, configure firewalls, set permissions.

  • Log Monitoring & Alerts: Tail logs, grep for errors, send notifications.

  • Backup and Cleanup Jobs: Scheduled scripts using cron.


🖥️ A Simple Example

bash
CopyEdit#!/bin/bash
# deploy.sh - A sample deployment script

echo "Starting deployment..."
git pull origin main
npm install
pm2 restart myapp

echo "Deployment complete ✅"

This basic script can be part of a post-merge hook or manually triggered after a feature is merged into the main branch.


🔒 Best Practices

  1. Use set -e to exit on any error.

  2. Log everything — redirect output to a log file.

  3. Use functions to modularize code.

  4. Parameterize — use arguments and flags.

  5. Test your scripts — especially destructive ones.


🛠️ Tools to Pair with Shell Scripts

  • cron – Schedule script execution

  • Ansible – Combine YAML and shell for idempotent automation

  • Docker – Entry scripts for container initialization

  • Jenkins – Run shell scripts in build steps


🚀 Real-Life Use Case: Automating AWS EC2 Backup

bash
CopyEdit#!/bin/bash
INSTANCE_ID="i-0abcd1234efgh5678"
DATE=$(date +%F)
aws ec2 create-image --instance-id $INSTANCE_ID --name "Backup-$DATE" --no-reboot

Automating AWS backups can be as simple as this script scheduled via cron.

#!/bin/bash

Create a new directory

mkdir my_directory echo "Directory 'my_directory' created."

Change into the new directory

cd my_directory echo "Changed to directory 'my_directory'."

Create a new file using touch

touch myfile.txt echo "File 'myfile.txt' created."

List files in the current directory

ls echo "Listed all files in the current directory."

Show present working directory

pwd echo "Displayed current working directory."

Write content to file using echo and redirect

echo "This is a test file." > myfile.txt echo "Content written to 'myfile.txt'."

Display content of the file

cat myfile.txt echo "Displayed content of 'myfile.txt'."

Show disk usage in human-readable format

df -h echo "Displayed disk usage."

Show memory usage

free -m echo "Displayed memory usage in MB."

Show number of CPU cores

nproc echo "Displayed number of CPU cores."

0
Subscribe to my newsletter

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

Written by

M Chidrup
M Chidrup

Certified Azure Cloud Enthusiast and Full Stack Developer with a strong foundation in building secure, scalable cloud-native applications. Passionate about integrating AI and automation in DevOps pipelines and exploring intelligent cloud systems. I specialize in React, Node.js, Azure, Kubernetes, and DevSecOps, and I love solving real-world problems through code, collaboration, and continuous learning.