lucian.marcuta
lucian.marcuta

Reputation: 1260

Bash: comparing two strings issues

I have written the following bash script:

#!/bin/bash
value="Maria Ion Gheorghe Vasile Maria Maria Ion Vasile Gheorghe"
value2="Maria Ion Gheorghe Vasile Maria Maria Ion Vasile Gheorghe"
if [[ "$value"!="$value2" ]]; then
        echo "different" 
else
        echo "match"
fi

The problem is that my script will always display "different" even though the strings stored in value and value2 variables are not different. What does bash actually compare?

And, another question related to this issue. Let`s say that we have:

v1 = grep 'a' a.txt
v2 = grep 'a' b.txt

Can we store and compare this variables if grep results are huge (lets say more than 50000 line for each variabile)?

~

Upvotes: 0

Views: 81

Answers (1)

arco444
arco444

Reputation: 22821

You need a space around the comparison operator in the condition:

if [[ "$value" != "$value2" ]]; then
        echo "different" 
else
        echo "match"
fi

If you don't do this, you're just testing a string - literally Maria Ion Gheorghe Vasile Maria Maria Ion Vasile Gheorghe!=Maria Ion Gheorghe Vasile Maria Maria Ion Vasile Gheorghe and the condition will always evaluate to true, thus yielding different.

Upvotes: 3

Related Questions