Step-by-Step Guide to Creating a Shell Script in Linux
14:00, 04.06.2026
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.