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
1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.