Shell Scripting Basics

Tejashree Salvi
6 min readMay 14, 2023

Day 4/90 with Shubham Londhe

As a DevOps engineer, you might be always looking for automating tasks, and streamline workflows. Here comes the shell scripting into the picture.

Pre-requisite for Shell Scripting: Linux Basics.

Let’s get started!

What is Kernel?

Linux kernel is responsible for managing system resources such as the CPU, memory, and input/output devices. When you start Firefox, the kernel interacts with the hardware to allocate system resources to the program and ensure that it runs smoothly.

What is Shell?

Shell is a program that provides a user interface to interact with the Linux OS. It allows the user to enter commands eg: ls (list the files), the shell interprets these commands and runs on behalf of the user.

Types of Shell in Linux

  1. Bash (Bourne-Again SHell): This is the most commonly used shell in Linux and is the default shell for most distributions.
  2. Zsh (Z SHell): similar to Bash, but offers additional features and customization options.
  3. Ksh (Korn SHell): compatible with both the Bash and C shells, and offers advanced scripting capabilities.
  4. Csh (C SHell): syntax similar to the C programming language and is popular among developers.
  5. Tcsh (TENEX C SHell): an improved version of the C shell, with additional features and improvements.

What is Linux Shell Scripting?

  • Shell Scripting is a process of writing scripts/programs that can be run on Linux CLI. It is written in shell language such as Bash which is used to automate repetitive tasks and many more.
  • Shell scripting is interpreted and not compiled. When a script is executed, the shell reads the scripts line by line, rather than compiling it at once and converting it into binary. This helps to modify the scripts without re-compiling. However, Shell scripts may run more slowly than compiled programs.
  • It is used to perform a variety of DevOps tasks, such as automating deployments, managing infrastructure, and deploying the code as well as monitoring.

Enough of theory, Let’s do some hands-on :)

Let’s create a shell script for automating the process of creating a new directory and changing it into the directory.


# create a new directory
mkdir scripts

#change to new directory
cd scripts
  • Save the file with .sh the extension. eg “”
  • Check the permission of the script file. It should have to execute permission chmod 777
  • To execute the script ./

What is Shebang?

In above code #!/bin/bash is called a Shebang.

  • Shebang is a special character in a script that tells the OS which interpreter to be used for executing a script. We will be using Bash Interpreter.
  • It starts with #! followed by the path of the interpreter. /bin/bash is the path to the interpreter executable file.

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge


echo "I will complete #90DaysOofDevOps challenge"

Here’s what the script does:

  • #!/bin/bash : is a Shebang that specifies the interpreter ie BASH
  • echo : used to print the message.

To run the script:

  • Save the code into a file
  • Make it executable: chmod +x
  • then execute the script ./

Input and Output in Shell Scripting

User Input:

echo "Enter your name?"
read name

echo "Hi $name, Thanks for reading the blog :)"

Here’s what the script does:

  • read name : command reads the user’s input and saves it to the $name variable.
  • echo : command outputs a message that includes the user’s name.

To run the script:

  • Save the code into a file
  • Make it executable: chmod +x
  • then execute the script ./

Command Line Argument:

Command Line Arguments are the parameters passed to the script while executing it.

  • Arguments are passed as a string and can be accessed using positional arguments.
  • $0 is used to store the file name, The first argument is accessed using $1 the second argument is accessed using $2 , and so on.
  • $# is used to get the total number of arguments.

echo "The file name is: $0"
echo "Name: $1"
echo "Age: $2"
echo "Follow for more such content. $1"
echo "The total number of arguments is: $#"

Control Structures

Control structure helps to execute the conditional execution of code.

The most used control structure in Shell Scripting is :

  1. If-else Statements
  2. For-loops

If-else Statements

The if-else statement allows for the conditional execution of code.

The Syntax for If-else Statements

if [ condition ]
# Code to be executed if condition is true
# Code to be executed if condition is false

Write a program using If else in Shell Scripting by comparing 3 numbers


# Declare the a, b, c Find the greater number among them.

echo "Let's find the greatest number!"
echo "a = $a, b = $b, c = $c"

if [[ $a -gt $b && $a -gt $c ]]
echo "A = $a is greater"
elif [[ $b -gt $a && $b -gt $c ]]
echo "B = $b is greater"
echo "C = $c is greater"

Here’s what the script does:

  • The if statement checks if the variable a is greater than both b and c using the -gt comparison operator, which stands for greater than. If this condition is true, it prints “A = $a is greater”.
  • If the first condition is false, the code checks the next condition using the elif statement, which checks if variable bis greater than both a and c. If this condition is true, it prints “B = $b is greater”.
  • If both the first and second conditions are false, the code executes the else block and prints “C = $c is greater”.

For loops

For loops allow for the repeated execution of code for a specific number of times

The Syntax for For Loops:

for variable in values
# Code to be executed

Write a program for displaying 1 to 10 numbers using For loops

echo "For loop to print 1 to 10 numbers!"
for i in {1..10}
echo "Value of i is: $i"


Functions are small blocks of code that are easier to read and perform specific tasks.


# Define a function for adding two numbers
add_numbers() {
sum=$(($1 + $2))
echo "The sum of $1 and $2 is: $sum"

# Call the function and pass two numbers as arguments
add_numbers 10 20