Shell scripting is one of the core skills for any DevOps engineer. It allows automation of repetitive tasks, system administration, monitoring, deployments, and integration with cloud and CI/CD tools.This article covers the must-know shell scripting concepts required to crack a DevOps trainee / associate / junior DevOps engineer role.A shell is a command-line interface (CLI) that acts as a bridge between the user and the operating system.Why is it Called a “Shell”?The kernel is the core of the operating systemThe shell surrounds the kernel and provides a way for users to interact with it that’s why it’s called a shell — it wraps around the kernel.You type a command (for example: The shell interprets the commandThe shell asks the OS (kernel) to execute itThe result is displayed back to youS…
Shell scripting is one of the core skills for any DevOps engineer. It allows automation of repetitive tasks, system administration, monitoring, deployments, and integration with cloud and CI/CD tools.This article covers the must-know shell scripting concepts required to crack a DevOps trainee / associate / junior DevOps engineer role.A shell is a command-line interface (CLI) that acts as a bridge between the user and the operating system.Why is it Called a “Shell”?The kernel is the core of the operating systemThe shell surrounds the kernel and provides a way for users to interact with it that’s why it’s called a shell — it wraps around the kernel.You type a command (for example: The shell interprets the commandThe shell asks the OS (kernel) to execute itThe result is displayed back to youShells are broadly classified into Graphical User Interface (GUI) shells and Command Line Interface (CLI) shells, based on how users interact with the operating system.Graphical User Interface (GUI) ShellA GUI shell allows users to interact with the system using windows, icons, menus, buttons, and a mouse, instead of typing commands.→ Windows: Windows Explorer (Windows Shell), Windows Desktop Environment→ GUI shells are user-friendly and commonly used on desktops but are rarely used on servers.Command to know which GUI shell is being usedecho $XDG_CURRENT_DESKTOPCommand Line Interface (CLI) ShellA CLI shell allows users to interact with the operating system by typing text-based commands through a terminal.→ Linux: bash (Bourne Again Shell), sh (Bourne Shell)→ Windows: command Prompt, powerShell→ Apple (macOS): zsh (default shell), terminal→ CLI shells are powerful, fast, and heavily used by system administrators and DevOps engineers.The original Unix shell that provides basic command execution and scripting features. It is lightweight and mainly used for compatibility and simple scripts.ii. bash (Bourne Again Shell): An enhanced version of sh with features like command history, auto-completion, and job control. It is the default shell in most Linux distributions.An advanced shell with powerful auto-completion, spelling correction, and theming support. It is popular among developers for its productivity features.Combines features of sh and csh with strong scripting capabilities. It is commonly used in enterprise and legacy Unix systems.Uses C-language-like syntax and provides built-in job control. It is less popular today due to scripting limitations.vi. fish (Friendly Interactive Shell): Designed for ease of use with smart suggestions and syntax highlighting. It focuses more on interactivity than scripting compatibility.Commands to know about Shell:→ echo $0 -> Shows the name of the shell currently in use.→ cat /etc/shells -> Lists all the shells available and installed on the Linux system.→ echo $SHELL -> Displays the default shell path assigned to the current user.→ It is an executable file containing several shell commands that executes sequentially.→ “.sh” is the extension for shell script files.Steps to Create and Run a Shell Script File:Note:–> File will execute from left to right–> Line by LineA variable is a named memory location used to store data that can be accessed and modified during script execution.Declaring and Assigning a Variable:syntax: declare_variable=assign_value #example: name=“Devops“Use $ before the variable name to access its value.#example: echo $name Taking Input from the User:The read command is used to take input from the user and store it in a variable.#example: read usernameExample Script: User Input and Variable Access:#!/bin/bashecho “Enter your name:” #Devopsecho “Welcome $name” Operators are symbols used to perform operations on variables and values. Used to perform mathematical calculations.#!/bin/bashecho enter the first valueread num1echo enter the second valueread num2sum=$((num1+num2)) #Additionsub=$((num1-num2)) #Subtractionprod=$((num1*num2)) #Multiplicationdiv=$((num1/num2)) #Division mod=$((num1%num2)) #Modulusecho Sub:$subecho Div:$div2. Used to compare two numeric values.→ (-gt or >) Greater than→ (-ge or >=) Greater than / Equal to→ (-le or b“:$((a>b)) #Greater thanecho “a =b”:$((a>=b)) #Greater than or Equal toecho “a 0)#!/bin/bashecho “Enter the file path:“read filepath if [ -e “$filepath” ]; thenelse echo “File does not exist“fiLooping statements are used to execute a block of code repeatedly until a certain condition is met. Executes a block of code for each item in a list or range.for variable in listdodone#!/bin/bashdodonefor((i=1;i What it does: Sends command output to a file and replaces old content.Example:echo “Hello” > file.txt #file.txt will contain only Hello2. >> What it does: Sends command output to a file and adds it at the end.Example:echo “World” >> file.txt #file.txt will now have Hello and World1. 2> What it does: Saves error messages into a fileExample:ls wrongfile 2> error.txt #Error message goes into error.txt,not on screen2. 2>> What it does: Adds error messages to a file without deleting old errorsExample:ls anotherwrongfile 2>> error.txt #New error is added to error.txt1. all.txt #Both success output & error go into all.txtWe are having special file in Linux → /dev/nullWhat it does: Throws away all the outputExample:ls > /dev/null #ls runs, but you see nothing on the screen.A Subshell is a child shell that runs a group of commands in isolation from the current shell.Created using parentheses ()Useful for isolating commandsChanges inside the subshell do not affect the main shell#!/bin/bashecho “Before subshell: $var” var=“Child” echo “Inside subshell: $var”)echo “After subshell: $var“Command substitution allows you to use the output of a command as a value for a variable or directly in a command.#!/bin/bashcurrent_date=$(date)echo “Today is $current_date”| Syntax | Purpose || —————–– | ———————————– || if command ; then | To check if a command is successful || ()| Subshell ||$()| Command substitution ||$(( ))| Arithmetic operators ||[]or[[]]` | Logical operators |Shell scripting is an essential skill for DevOps engineers to automate system tasks and improve efficiency. It is widely used for system administration, monitoring, backups, and CI/CD automation.Learning core concepts like variables, conditions, loops, and functions builds a strong scripting foundation. Mastering shell scripting helps reduce manual effort and prepares candidates for real-world DevOps roles.If you found this helpful, please to hit the and buttons to help me write more articles like this.You can also check out my previously written Linux article for a comprehensive understanding of Linux concepts.