简体   繁体   中英

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)?

~

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM