0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check Palindrome Number with Output

Use a Bash script that reverses the number string with rev and compares it to the original using if [ "$num" = "$rev" ] to check if it's a palindrome.
📋

Examples

Input121
Output121 is a palindrome number.
Input12321
Output12321 is a palindrome number.
Input123
Output123 is not a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, treat it as a string and reverse it. Then compare the reversed string with the original. If they match exactly, the number is a palindrome.
📐

Algorithm

1
Get the input number as a string.
2
Reverse the string representation of the number.
3
Compare the reversed string with the original string.
4
If they are equal, print that the number is a palindrome.
5
Otherwise, print that it is not a palindrome.
💻

Code

bash
#!/bin/bash
read -p "Enter a number: " num
rev=$(echo "$num" | rev)
if [ "$num" = "$rev" ]; then
  echo "$num is a palindrome number."
else
  echo "$num is not a palindrome number."
fi
Output
Enter a number: 121 121 is a palindrome number.
🔍

Dry Run

Let's trace the input 121 through the code

1

Input number

num = 121

2

Reverse number string

rev = 121

3

Compare original and reversed

121 == 121 → true

numrevcomparison result
121121true
💡

Why This Works

Step 1: Read input as string

The number is read as a string so it can be reversed easily using string commands.

Step 2: Reverse the string

The rev command reverses the string characters to check palindrome property.

Step 3: Compare original and reversed

If the reversed string matches the original, the number reads the same forwards and backwards, so it is a palindrome.

🔄

Alternative Approaches

Manual reversal using loop
bash
#!/bin/bash
read -p "Enter a number: " num
rev=""
len=${#num}
for (( i=len-1; i>=0; i-- )); do
  rev+=${num:i:1}
done
if [ "$num" = "$rev" ]; then
  echo "$num is a palindrome number."
else
  echo "$num is not a palindrome number."
fi
This avoids external commands but is longer and less concise.
Using arithmetic to reverse number
bash
#!/bin/bash
read -p "Enter a number: " num
original=$num
rev=0
while [ $num -gt 0 ]; do
  digit=$(( num % 10 ))
  rev=$(( rev * 10 + digit ))
  num=$(( num / 10 ))
done
if [ $original -eq $rev ]; then
  echo "$original is a palindrome number."
else
  echo "$original is not a palindrome number."
fi
This uses math operations instead of string reversal, good for numeric palindrome checks.

Complexity: O(n) time, O(n) space

Time Complexity

Reversing the string takes time proportional to the length of the number string, so O(n).

Space Complexity

The reversed string requires extra space proportional to the input length, so O(n).

Which Approach is Fastest?

Using rev is concise and efficient for strings, while arithmetic reversal avoids external commands but is more complex.

ApproachTimeSpaceBest For
Using rev commandO(n)O(n)Simple and readable string reversal
Manual loop reversalO(n)O(n)Avoids external commands, more control
Arithmetic reversalO(n)O(1)Numeric palindrome checks without strings
💡
Use the rev command to quickly reverse strings in Bash for palindrome checks.
⚠️
Forgetting to treat the number as a string causes incorrect palindrome checks when using numeric operations.