Shell Scripting in Linux for Beginners

Shell Scripting in Linux for Beginners with Practical Examples

Shell scripting is one of the most commonly used skills in DevOps. It is mainly used on Linux systems and helps automate tasks, which reduces manual work and human involvement.

Even today, there are many automation tools and programming languages like Python. Still, shell scripting remains widely used in the industry because it is simple, fast, lightweight, and efficient.

This is the first blog in the shell scripting series. In this blog, we will cover an introduction to the shell along with some basic concepts.

You will learn about the shell, variables, user input, command-line arguments, and basic arithmetic operations. By the end of this blog, you will have a basic understanding of shell scripting.


What is Shell and Shell Scripting?

A shell is a program that allows users to interact with the operating system. When you type commands in the Linux terminal, the shell reads those commands and instructs the operating system to perform the required actions.

Linux supports different types of shells, such as:

  • Bash (Bourne Again Shell)
  • sh (Bourne Shell)
  • zsh

Among these, Bash is the most commonly used shell and is the default shell in most Linux distributions.

A shell script is a collection of shell commands written inside a single file.


Writing Your First Shell Script

Step 1: Create a Shell Script File

touch hello.sh

Step 2: Add the Shebang Line

#!/bin/bash

Step 3: Write a Simple Command

echo "Hello, Shell Scripting!"

Step 4: Make the Script Executable

chmod +x hello.sh

Step 5: Run the Script

./hello.sh

Output:

Hello, Shell Scripting!


echo Command

echo "Hello World"

Output:

Hello World

To print in New Line

echo -e "Hello\nWorld"

Output:

Hello
World

Print Tab Space

echo -e "Hello\tWorld"

Output:

Hello    World

No New Line at the End

echo -n "Hello World"; echo " Hi"

Output:

Hello World Hi


Variables in Shell Scripting

name="Linux"
 echo "Welcome to $name"

Output:

Welcome to Linux


User-Defined Variables

city="Hyderabad"
echo "City is $city"

Output:

City is Hyderabad


Environment Variables

env

Output:

HOME=/home/user
USER=user
PATH=/usr/local/bin:/usr/bin:/bin

Access an Environment Variable

echo $HOME

Output:

/home/user


Taking User Input

echo "Enter your name:"
read username
echo "Welcome $username"

Input: Venkat
Output:

Welcome Venkat

Input with Prompt

read -p "Enter your name: " name
echo "Hello $name"

Input: Bye

Hello Bye

Password Input

read -sp "Enter password: " password
echo
echo "Password saved"

Output:

Password saved

Default REPLY Variable

read
echo "You entered: $REPLY"

Input: Linux

You entered: Linux


Command-Line Arguments

./script.sh AWS DevOps
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"
echo "All arguments: $@"

Output:

Script name: ./script.sh
First argument: AWS
Second argument: DevOps
Total arguments: 2
All arguments: AWS DevOps


Arithmetic Operations in Shell Scripting

a=10
b=5

echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"

Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2


Execution Flow of a Shell Script

A shell script runs commands in a linear manner. The shell reads the script from top to bottom and executes each command one by one in the order they appear.

Example

#!/bin/bash
echo "Script Started"
echo "Script Stopped"

Output:

Script Started
Script Stopped

This example shows that the first command executes before the second command. The execution always follows the written order unless conditions or loops are used.


Comments in Shell Scripts

Comments are used to explain what a script does or to describe specific lines of code. They improve readability and make scripts easier to understand and maintain.

In shell scripting, comments start with the # symbol. The shell ignores comment lines and does not execute them.

Single-Line Comment Example

# This is a comment
echo "Hello Linux"

Output:

Hello Linux

Multiple-Line Comments

Shell scripting does not support true multi-line comments. To write comments on multiple lines, use the # symbol at the beginning of each line.

# This script prints a message
# It is written for beginners
# Used to understand comments
echo "Shell scripting is simple"

Output:

Shell scripting is simple


Exit Status in Shell Scripting

Every command executed in Linux returns an exit status. This value indicates whether the command ran successfully or failed.

An exit status of 0 means the command was successful. Any non-zero value indicates an error or failure.

Example

ls /
echo $?

Output:

0

Here, $? stores the exit status of the previously executed command. Exit status is important when writing scripts that require basic error checking.

Be the first to comment

Leave a Reply

Your email address will not be published.


*