Yann
Yann

Reputation: 13

If else statement in bash script

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

Upvotes: 1

Views: 133

Answers (1)

John1024
John1024

Reputation: 113994

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

Upvotes: 1

Related Questions