Skip to main content

Command Palette

Search for a command to run...

๐Ÿš€ DevSecOps Journey โ€” Day 8

Published
โ€ข4 min read
๐Ÿš€ DevSecOps Journey โ€” Day 8

Mastering Shell Scripting for DevOps Automation

โœจ Building the foundations for reliable automation workflows

Shell scripting is one of the most essential skills in DevOps & DevSecOps.
Today, I explored how shells work, why automation matters, and how scripting makes deployments, server setups, and CI/CD processes more efficient and reliable.


๐Ÿš What is a Shell?

A shell is a command-line interface (CLI) that allows users to interact with the operating system and execute commands.

๐Ÿ‘‰ Common Shell Types

  • ๐ŸŸฆ bash

  • ๐ŸŸจ sh

  • ๐ŸŸช zsh

  • ๐Ÿ python shell

Shells allow us to run commands, automate workflows, and build scripts that execute consistently across environments.


โš™๏ธ Why Shell Scripting Matters in DevOps

In real-world DevOps environments:

  • Installing Docker

  • Configuring Jenkins

  • Deploying applications

  • Running validations

โ€ฆoften require multiple manual commands.

With Shell Scripting, we can:
โœ”๏ธ Combine all commands into one file
โœ”๏ธ Run them in a single execution
โœ”๏ธ Share scripts across teams
โœ”๏ธ Automate builds, deployments & server provisioning
โœ”๏ธ Reduce human error

Shell scripting = faster, safer, predictable automation.


๐Ÿงญ Understanding the Shebang (#!)

The first line of most shell scripts is:

#!/bin/bash
  • #! โ†’ Shebang

  • /bin/bash โ†’ tells OS to execute this script using bash

If you skip the shebang, the script can still run with:

bash filename.sh

๐Ÿงฉ Variables & Basic Operations

โœ”๏ธ Declaring Variables

name="Jayesh"
echo "Hello $name"

age="24"
echo "Your age is $age"

โœ”๏ธ Arithmetic

x=4
y=7
sum=$((x+y))
echo "$sum is "

โœ”๏ธ String Concatenation

greetings="Hello"
name="Jayesh"
message="$greetings, $name"
echo "$message"

โœ”๏ธ String Length

string="Vishal Patil"
length=${#string}
echo "Length : $length"

๐Ÿ“ Echo Behavior & Special Characters

variable="Hello World"
echo "$variable"
echo '$variable'
echo $variable
echo "both '$variable'"

special_char="\$"
echo "special char - $special_char"

๐Ÿ” User Input Handling

โœ”๏ธ Hidden Password Input

read -s -p "Your password is " password
echo "Password has been set $password"

โœ”๏ธ Timeout-Based Input

read -t 6 -p "Enter something in 6 second " timed_out
echo "$timed_out"

๐Ÿง  Conditional Statements

num=10

if [ $num -gt 10 ]; then
    echo "Number is greater than 10"
elif [ $num -eq 10 ]; then
    echo "Number is equal to 10"
else
    echo "Number is less than 10"
fi

๐Ÿ”€ Switch / Case Statements

fruit="apple"

case $fruit in
    "apple")
        echo "it is an apple"
        ;;
    "banana")
        echo "it is banana"
        ;;
    "mango")
        echo "it is mango"
        ;;
    *)
        echo "Unknown fruit"
        ;;
esac

๐Ÿ” Loops in Shell Scripting

โœ”๏ธ For Loop โ€” Numeric

for i in {1..21}; do
    echo "numbers $i"
done

โœ”๏ธ For Loop โ€” Array

fruits=("shimpi" "patil" "deshmukh")
for fruit in "${fruits[@]}"; do
    echo "fruits name $fruit"
done

โœ”๏ธ While Loop

counter=1
while [ $counter -le 5 ]; do
    echo "count : $counter"
    counter=$((counter+1))
done
echo "loop finished"

โœ”๏ธ Until Loop

counter=1
until [ $counter -ge 5 ]; do
    echo "count : $counter"
    counter=$((counter+1))
done
echo "loop finished"

๐Ÿงฎ Functions โ€” Local & Global Variables

โœ”๏ธ Local Variable Example

calculate_sum() {
    local num1="$1"
    local num2="$2"
    local sum=$((num1+num2))
    echo "$sum"
    return "$sum"
}

result=$(calculate_sum 10 20)
echo "sum is $result"

โœ”๏ธ Global vs Local

global_var="Im global variable"

local_variable() {
    local local_var="Hello I am local"
    echo "Inside function $local_var"
    echo "outside function $global_var"
}

local_variable

echo "1111 $local_var"   # Not accessible
echo "2222 $global_var"  # Accessible

โœ‚๏ธ String Manipulation

โœ”๏ธ Extract Substring

string="Hello,world"
substring=${string:6:5}
echo "$substring"

โœ”๏ธ Replace Text

string1="ms dhoni, def"

search="def"
replace="faf du plessis"

result=${string1//$search/$replace}

echo "org $string1"
echo "org $result"

๐ŸŽฏ Why Shell Scripting Matters in DevOps/DevSecOps

Shell scripting powers automation for:

  • โš™๏ธ CI/CD pipelines

  • ๐Ÿš€ Automated deployments

  • ๐Ÿ“ฆ Server provisioning

  • ๐Ÿ“Š Log analysis

  • ๐Ÿ” Security checks

  • โ˜๏ธ Cloud operations

  • ๐Ÿ” System monitoring

Mastering shell scripting = mastering DevOps automation.


โœ… Day 8 Summary

Today, I learned and practiced:

  • Variables, arithmetic & strings

  • User input, password, timeout

  • Conditionals & switch-case

  • Loops (for, while, until)

  • Functions + local/global variables

  • String manipulation (extract, replace)

These scripting fundamentals are essential for writing automation scripts used in deployments, CI/CD workflows, cloud platforms, and secure engineering systems.

More from this blog

D

DevSecOps - Zero To Hero

19 posts