In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set C using shell scripting.
We will learn how to check whether a given string is a palindrome or not using different shell scripting approaches.
📌 Practical Question
Write a script to check whether a given string is palindrome or not.
🧠 What is a Palindrome?
A palindrome is a word or string that reads the same from left to right and right to left.
Examples
madam
level
radar
racecar
For example:
madam → madam
Since the original string and reversed string are the same, madam is a palindrome.
Non-Palindrome Examples
hello
linux
computer
For example:
hello → olleh
Since they are different, hello is not a palindrome.
✅ Solution 1: Using for Loop
This solution checks the string character by character and builds its reverse.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
reverse=""
for ((i=${#str}-1; i>=0; i--))
do
ch=${str:$i:1}
reverse="$reverse$ch"
done
echo "Original String : $str"
echo "Reverse String : $reverse"
if [ "$str" = "$reverse" ]
then
echo "String is Palindrome"
else
echo "String is Not Palindrome"
fi
🔍 Explanation
Step 1: Take Input
read str
The user-entered string is stored in the variable str.
Step 2: Initialize Reverse String
reverse=""
We start with an empty string.
Characters will be added to this variable one by one.
Step 3: Find String Length
${#str}
This returns the number of characters in the string.
For example:
madam
has:
5 characters
Therefore, the last character is at index:
4
because Bash string indexing starts from 0.
Step 4: Loop from Last Character to First
for ((i=${#str}-1; i>=0; i--))
For:
madam
the indexes are:
m → 0
a → 1
d → 2
a → 3
m → 4
The loop starts from 4 and moves toward 0.
So the characters are read as:
m
a
d
a
m
Step 5: Extract One Character
ch=${str:$i:1}
This extracts one character from the string.
The general syntax is:
${string:start:length}
For example:
${str:2:1}
means:
Start at index
2and extract1character.
Step 6: Build the Reverse
reverse="$reverse$ch"
Each extracted character is added to the reverse variable.
For example:
Original: hello
Reverse building:
o
ol
oll
olle
olleh
Step 7: Compare Strings
if [ "$str" = "$reverse" ]
If the original string and reversed string are equal, the string is a palindrome.
🖥️ Example Output
Example 1
Enter a string:
madam
Original String : madam
Reverse String : madam
String is Palindrome
Example 2
Enter a string:
hello
Original String : hello
Reverse String : olleh
String is Not Palindrome
✅ Solution 2: Using rev Command
Linux provides a command called rev that reverses characters in each line.
This makes the palindrome program much shorter.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
reverse=$(echo "$str" | rev)
echo "Original String : $str"
echo "Reverse String : $reverse"
if [ "$str" = "$reverse" ]
then
echo "String is Palindrome"
else
echo "String is Not Palindrome"
fi
🔍 Understanding rev
Consider:
echo "madam" | rev
Output:
madam
And:
echo "hello" | rev
Output:
olleh
Therefore:
reverse=$(echo "$str" | rev)
stores the reversed string in the reverse variable.
Then we simply compare:
if [ "$str" = "$reverse" ]
🖥️ Example Output
Enter a string:
level
Original String : level
Reverse String : level
String is Palindrome
✅ Solution 3: Character-by-Character Comparison
Instead of creating a separate reverse string, we can compare characters from both ends.
Shell Script
#!/bin/bash
echo "Enter a string:"
read str
length=${#str}
isPalindrome=1
for ((i=0, j=length-1; i<j; i++, j--))
do
if [ "${str:$i:1}" != "${str:$j:1}" ]
then
isPalindrome=0
break
fi
done
if [ $isPalindrome -eq 1 ]
then
echo "String is Palindrome"
else
echo "String is Not Palindrome"
fi
🔍 How This Solution Works
Suppose the input is:
madam
The script compares:
m ↔ m
a ↔ a
d ↔ d
All characters match, so the string is a palindrome.
For:
hello
the first comparison is:
h ↔ o
They are different, so the script immediately stops.
break
This avoids unnecessary comparisons.
📊 Comparing the Solutions
| Method | Difficulty | Extra Command | Good for Practical |
|---|---|---|---|
for + Reverse |
⭐⭐ | No | ✅ Excellent |
rev |
⭐ Easy | rev |
✅ Excellent |
| Character Comparison | ⭐⭐⭐ | No | ✅ Good |
Which one should you use?
For a practical examination:
Beginner-friendly: Use the rev solution.
To demonstrate shell scripting logic: Use the for loop solution.
For better algorithmic understanding: Use character-by-character comparison.
📝 Quick Revision
Reverse using rev
reverse=$(echo "$str" | rev)
String length
${#str}
Extract one character
${str:$i:1}
Compare strings
if [ "$str" = "$reverse" ]
Break the loop
break
⚠️ Important Note About Spaces and Case
The basic solutions above treat the input as a normal string.
For example:
Madam
is not considered the same as:
madam
because uppercase and lowercase characters are different.
Similarly:
nurses run
contains a space, so it is different from:
nursesrun
If the practical specifically requires case-insensitive palindrome checking or ignoring spaces, the script needs a small modification.
🎯 Practical Exam Tips
Remember these important points:
- A palindrome is the same when read forward and backward.
- Bash string indexes start from
0. -
${#str}gives the string length. -
${str:$i:1}extracts a character. -
revcan reverse a string quickly. - Always quote string variables when comparing them.
-
breakcan stop the loop as soon as a mismatch is found.
📚 Related Linux Practical Sets
This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.
- Set A – Simple Interest & Compound Interest
- Set B – Vowels Count & Case Conversion
- Set C – Palindrome String
- Set D – File Operations & Text Processing
- Set E – Display Lines with Validation
- Set F – Count Files & Directories
- Set G – Recently Modified Files
- Set H – Employee Gross Salary
🐧 Conclusion
The Palindrome String practical is a simple but important shell scripting exercise.
It helps students understand:
- Strings
- Loops
- String indexing
- Conditions
- Command substitution
- Linux commands
Try writing the program yourself using the for loop approach first. After understanding the logic, try the shorter rev version.
Happy Learning & Best of Luck for your Linux Practical! 🐧💻
Top comments (0)