Step-by-Step Guide to Creating a Shell Script in Linux

watch 3m, 5s
views 2

14:00, 04.06.2026

Article Content
arrow

  • Writing a Shell Script
  • Adding Comments in a Shell Script
  • Working with Variables in Shell Scripting
  • Specifying the Shell Script Interpreter
  • Using Comparison Operators
  • Comparing Integer Values 
  • Integer Comparison
  • Comparing Strings in Shell Scripts 
  • String Comparison
  • Implementing Conditional Statements  
  • Using the If Statement
  • Implementing If-Else Logic
  • Understanding Loops in Shell Scripts
  • The While Loop
  • The For Loop
  • Handling Positional Arguments in Scripts
  • Capturing and Storing Command Outputs
  • Understanding Exit Codes in Shell Commands

Shell scripting is a great way to automate tasks and run commands efficiently in Linux. This guide will help you get started from the basics, even if you are new to it.

Writing a Shell Script

To create a shell script, open any text editor like nano, vim, or gedit

Type your commands and save the file with a .shextension.

Example:

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

To make the script executable, use this command:

chmod +x myscript.sh

Then run it:

./myscript.sh

Adding Comments in a Shell Script

Comments help explain your code. In shell scripts, comments start with #.

Example:

# This is a comment
echo "Running the script..."

Use comments to make your code easier to understand for yourself and others.

Working with Variables in Shell Scripting

Variables store values that you can reuse.

Example:

name="Alice"
echo "Hello, $name"

There’s no need to declare the type. You can directly assign and use the variable.

Specifying the Shell Script Interpreter

The first line in a shell script is called the shebang. It tells the system what interpreter to use.

Example:

#!/bin/bash

Always include this line at the top of your script for it to run properly.

Using Comparison Operators

Comparison operators let you compare values, which is useful for decision making in scripts.

Comparing Integer Values 

For numeric comparisons, use these operators:

Integer Comparison

Operator

Description

-eq

is equal to

-ne

is not equal to

-gt

is greater than

-ge

is greater than or equal to

-lt

is less than

-le

is less than or equal to



Example:

a=10
b=5
if [ $a -gt $b ]; then
  echo "$a is greater than $b"
fi

Comparing Strings in Shell Scripts 

To compare text values (strings), use these operators:

String Comparison

Operator

Description

==

is equal to

!=

is not equal to

\<

is less than, in ASCII alphabetical order

\>

is greater than, in ASCII alphabetical order

Example:

str1="apple"
str2="banana"
if [ "$str1" \< "$str2" ]; then
  echo "$str1 comes before $str2 alphabetically"
fi

Implementing Conditional Statements  

Conditional statements let your script choose what to do based on a condition.

Using the If Statement

The if statement checks a condition and runs commands if it’s true.

Example:

if [ -e myfile.txt ]; then
  echo "File exists"
fi

Implementing If-Else Logic

You can use else to handle when a condition is false.

Example:

if [ -d myfolder ]; then
  echo "Folder exists"
else
  echo "Folder does not exist"
fi

You can also use elif for additional conditions:

if [ $a -gt $b ]; then
  echo "A is greater"
elif [ $a -eq $b ]; then
  echo "A is equal"
else
  echo "A is less"
fi

Understanding Loops in Shell Scripts

Loops let you repeat commands multiple times. There are two common loops: while and for.

The While Loop

The while loop runs while the condition is true.

Example:

count=1
while [ $count -le 5 ]; do
  echo "Count is $count"
  ((count++))
done

The For Loop

The for loop iterates over a list or range.

Example:

for i in 1 2 3 4 5; do
  echo "Number: $i"
done
You can also loop through files:
for file in *.txt; do
  echo "Found file: $file"
done

Handling Positional Arguments in Scripts

Shell scripts can accept arguments from the command line.

Example:

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

Run the script:

./myscript.sh hello world

Output:

Script name: ./myscript.sh
First argument: hello
Second argument: world

Capturing and Storing Command Outputs

You can capture the result of a command using $(command).

Example:

current_date=$(date)
echo "Today is $current_date"

This stores the output of date in a variable.

Understanding Exit Codes in Shell Commands

Every command returns an exit code. You can check it using $?.

  • 0 means success.
  • Any other value means there was an error.

Example:

ls /notfound
if [ $? -ne 0 ]; then
  echo "Directory not found"
fi

This helps in detecting failures in scripts.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
75 GB SSD
Bandwidth
Bandwidth
Unlimited
wKVM-SSD 2048 Windows

10.23 /mo

/mo

Billed annually

-9.6%

CPU
CPU
8 Xeon Cores
RAM
RAM
32 GB
Space
Space
200 GB SSD
Bandwidth
Bandwidth
12 TB
wKVM-SSD 32768 Metered Windows

156 /mo

/mo

Billed annually

-18.6%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
4 TB
wKVM-SSD 4096 Metered Windows

38 /mo

/mo

Billed annually

-24.4%

CPU
CPU
2 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
300 GB
KVM-SSD 1024 HK Linux

13 /mo

/mo

Billed annually

-16.2%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
60 Mbps
DDoS Protected SSD-KVM 4096 Linux

67 /mo

/mo

Billed annually

-8.4%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
75 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-wKVM-SSD 2048 Windows

37.4 /mo

/mo

Billed annually

-24.7%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
4 TB
KVM-SSD 4096 Metered Linux

31 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 8192 Linux

25.85 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
30 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 2048 Linux

30.3 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Epyc Cores
RAM
RAM
8 GB
Space
Space
100 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 8192 Windows

28.99 /mo

/mo

Billed annually

Other articles on this topic

cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.