[ACCEPTED]-Linux shell programming string compare syntax-compare
Accepted answer
The single equal is correct
string1 == string2
string1 3 = string2
True if the strings are equal. ‘=’ should 2 be used with the test command for POSIX 1 conformance
NAME="rafael"
USER="rafael"
if [ "$NAME" = "$USER" ]; then
echo "Hello"
fi
In general, the = operator works the same 3 as == when comparing strings.
Note: The == comparison 2 operator behaves differently within a double-brackets 1 test than within single brackets.
[[ $a == z* ]] # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).
These pages explain the various comparison 2 operators in bash:
- http://www.tech-recipes.com/rx/209/bournebash-shell-scripts-string-comparison/
- http://tldp.org/LDP/abs/html/comparison-ops.html
- http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html#ss11.2
On the second linked page, you 1 will find:
==
is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.