0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check Palindrome String Easily

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

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome, think of reading it forwards and backwards. If both are the same, the string is a palindrome. So, reverse the string and compare it to the original.
📐

Algorithm

1
Get the input string from the user
2
Reverse the input string
3
Compare the original string with the reversed string
4
If they are the same, print it is a palindrome
5
Otherwise, print it is not a palindrome
💻

Code

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

Dry Run

Let's trace the input 'madam' through the code

1

Input string

User enters 'madam'

2

Reverse string

Reverse of 'madam' is 'madam'

3

Compare strings

'madam' equals 'madam', so it is a palindrome

Original StringReversed StringPalindrome?
madammadamYes
💡

Why This Works

Step 1: Reversing the string

The rev command reverses the characters in the string, making it easy to compare.

Step 2: Comparing strings

Using [ "$str" = "$rev" ] checks if the original and reversed strings are exactly the same.

Step 3: Output result

If they match, the script prints the string is a palindrome; otherwise, it prints it is not.

🔄

Alternative Approaches

Using a loop to compare characters
bash
#!/bin/bash
read -p "Enter a string: " str
len=${#str}
palindrome=true
for (( i=0; i<len/2; i++ )); do
  if [ "${str:i:1}" != "${str:len-i-1:1}" ]; then
    palindrome=false
    break
  fi
done
if $palindrome; then
  echo "$str is a palindrome"
else
  echo "$str is not a palindrome"
fi
This method avoids external commands but is longer and more complex.
Using parameter expansion to reverse string
bash
#!/bin/bash
read -p "Enter a string: " str
rev=""
for (( i=${#str}-1; i>=0; i-- )); do
  rev+=${str:i:1}
done
if [ "$str" = "$rev" ]; then
  echo "$str is a palindrome"
else
  echo "$str is not a palindrome"
fi
Pure Bash approach without external commands but less efficient.

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

Time Complexity

The script reverses the string once, which takes O(n) time where n is the string length.

Space Complexity

It stores the reversed string, so space used is O(n).

Which Approach is Fastest?

Using rev is fastest and simplest; loop methods are slower and more complex.

ApproachTimeSpaceBest For
Using rev commandO(n)O(n)Simple and fast palindrome check
Loop character comparisonO(n)O(1)No external commands, more control
Parameter expansion reverseO(n)O(n)Pure Bash, no external tools
💡
Use the rev command for a simple and fast palindrome check in Bash.
⚠️
Forgetting to quote variables in comparisons can cause errors or unexpected results.