1

I tried lot of ways to match a string but my if statement don't work. I want to test if the first parameter is equal to his reverse.

For example if [ $1 = "something" ] may work but i don't know how to do it if i'm using my reverse variable

 #!/bin/bash
 echo "la string en parametre" ${1}
 reverse= echo -n $1 | rev
 if [[ $1=reverse ]]; then
 echo "est pas un palindrome"
 else
 echo "est un palindrome"
 fi
Yann
  • 13
  • 4
  • possible duplicate of [How do I compare two string variables in an 'if' statement in Bash?](http://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash) – William Price Feb 21 '15 at 05:35

1 Answers1

1

First, this doesn't work:

reverse= echo -n $1 | rev

Use command substitution:

reverse=$( echo -n "$1" | rev )

Second, this won't work:

if [[ $1=reverse ]]; then

There must be spaces around = and to access a variable, you need a dollar sign:

if [[ $1 = $reverse ]]; then

In sum, try:

echo "la string en parametre: '$1'"
reverse=$( echo -n "$1" | rev )
if [[ $1 = $reverse ]]; then
    echo "est un palindrome"
else
    echo "est pas un palindrome"
fi
John1024
  • 109,961
  • 14
  • 137
  • 171