Day 8 of 90 Days of DevOps Challenge: Shell Scripting Fundamentals

Vaishnavi DVaishnavi D
5 min read

What is Shell Scripting?

Shell scripting is the process of writing a series of commands in a plain text file, known as a script, that is executed by the Unix/Linux shell, the command-line interpreter.

These scripts automate repetitive tasks such as file management, software installation, system monitoring, and more, making them essential tools for system administrators, DevOps engineers, and developers.

Key Benefits of Shell Scripting:

  • Saves time by automating routine system tasks

  • Improves consistency and reduces human error

  • Integrates easily with cron jobs for scheduled tasks

  • Offers better control over system processes and resource management

Types of Shells

1. Bash (Bourne Again Shell)

  • Path: /bin/bash

  • Most commonly used shell in Linux.

  • Supports command history, auto-completion, scripting features, and more.

  • Default shell on most Linux distributions.

2. Sh (Bourne Shell)

  • Path: /bin/sh

  • The original Unix shell developed by Stephen Bourne.

  • Lightweight and POSIX-compliant but lacks advanced features of Bash.

3. Zsh (Z Shell)

  • Path: /bin/zsh

  • Popular among power users and developers.

  • Features: better auto-completion, spelling correction, and customization.

  • Commonly used with the Oh My Zsh framework.

4. Ksh (Korn Shell)

  • Path: /bin/ksh

  • Developed by David Korn.

  • Combines features of both Bash and C shell.

  • Used in enterprise systems and scripting environments.

5. Csh (C Shell)

  • Path: /bin/csh

  • Syntax similar to the C programming language.

  • Supports job control and scripting but is less user-friendly than Bash.

6. Tcsh

  • An enhanced version of Csh with command-line editing, history, and more.

  • Not widely used today but still supported on many systems.

How to check the shell type and available shells in your system?

echo $SHELL      # check which shell you are using
cat /etc/shells  # check available shells

The shebang line

The shebang (written as #!) is a special character sequence placed at the very top of a script file that tells the operating system which interpreter to use to execute the script.

Syntax:

#!/path/to/interpreter

Why is Shebang Important?

  • Makes it easier to run: You can just type ./script.sh instead of writing bash script.sh every time.

  • Makes sure it runs correctly: It tells the computer exactly which program (like Bash) should run the script, no matter what the system’s default shell is.

Every shell script should start with a shebang line that tells the system which interpreter to use:

#!/bin/bash               # Bash shell, Most common for Linux scripts
#!/bin/sh                  # Bourne shell, Lightweight POSIX-compatible
#!/usr/bin/env python3      # Python interpreter, Used in Python scripts
#!/usr/bin/perl           #    Perl interpreter, for  pearl scripting

Writing Your First Script

Let's create a file called hello.sh:

#!/bin/bash
echo "Hello, DevOps World!"

Make it executable:

chmod +x hello.sh  # give execute permission 
./hello.sh         # run script

Variables

In Shell Scripting (especially Bash), variables are used to store and retrieve data such as strings, numbers, or command outputs.

There are three main types of variables:

1. User-Defined Variables

These are the most common and are created by users to store custom data.

Syntax:

name="ZerotoRoot"
course="DevOps"

Access:

echo $name
echo $course

2. Environment Variables

Environment variables are system-wide variables used to configure the shell environment. They are automatically available to all child processes of the shell.

Examples:

echo $HOME      # Home directory of the user
echo $PATH      # Directories to search for executables
echo $USER      # Currently logged-in user

To create your own environment variable:

export project="MyProject"

3. Special Variables (Shell-defined)

These are predefined by the Bash shell and used mainly in shell scripts to get information about input arguments, processes, and execution status.

VariableDescription
$0Name of the script
$1, $2...$nArguments passed to the script
$#Number of arguments passed
$@All arguments (as a list)
$*All arguments (as a single string)
$?Exit status of the last command
$$Process ID of the current shell
$!Process ID of the last background command

User Input and Output

read -p "Enter your name: " user_name
echo "Welcome, $user_name!"

Displaying System Info

echo "Today is: $(date)"
echo "System Uptime: $(uptime)"

Hands-on Practice

1. Script to Greet the User

#!/bin/bash

echo "Enter your name:"
read name
echo "Hello, $name! Hope you're having a great day. 😊"

2. Simple Calculator Script (Addition)

#!/bin/bash

read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"

3. Script to Display Current Date and System Uptime

#!/bin/bash

echo "📅 Today's date is: $(date)"
echo "⏱️ System uptime: $(uptime -p)"

Final Thoughts

Understanding these basics gave me a solid entry point into automating system tasks. With just a few lines of Bash, you can simplify your daily DevOps workflows. it’s not just about writing code, it’s about thinking in terms of efficiency and repeatability. I now see how it plays a key role in DevOps, system administration, and cloud workflows.

I’m excited to keep practicing and building more scripts to make daily tasks simpler and smarter. This is just the beginning, and I’m looking forward to exploring more powerful features in the coming days.

Let’s keep learning and growing, one script at a time!

4
Subscribe to my newsletter

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

Written by

Vaishnavi D
Vaishnavi D