This question already has answers here:
What is the difference between operator "=" and "==" in Bash?
(2 answers)
Closed 7 years ago.
In bash, what's the difference, if any, between the equal and double equal test operators?
[[ "a" = "a" ]] && echo equal || echo not-equal
[[ "a" == "a" ]] && echo equal || echo not-equal
[[ "a" = "b" ]] && echo equal || echo not-equal
[[ "a" == "b" ]] && echo equal || echo not-equal
results in:
equal
equal
not-equal
not-equal
There's no difference, == is a synonym for = (for the C/C++ people, I assume). See here, for example.
You could double-check just to be really sure or just for your interest by looking at the bash source code, should be somewhere in the parsing code there, but I couldn't find it straightaway.
Related
This question already has answers here:
Bash regex =~ operator
(3 answers)
Regex matching in a Bash if statement
(4 answers)
How can I match a string with a regex in Bash?
(6 answers)
Simple logical operators in Bash
(5 answers)
Closed 2 years ago.
Please help.
I'm trying to compare string1 against string2 in a Bash script.
I can do the easy bit of:-
if [[ $string1 == $string2 ]]
then
yippee
fi
What I'm having trouble with is the syntax for when
"the$string1" == $string2 or "a$string1" == $string2
or
$string1 == the$string2 or $string1 == a$string2
I assume it's something like:-
if [[ $string1 == $string2 || "(a|the)$string1" == $string2 || $string1 == "(a|the)$string2" ]]
But it's not and I cannot seem to find the answer. (I'm obviously asking the wrong question!)
Thanks for any help.
PS I'd rather not use any external progs such as awk etc.
You might want:
if [[ $string1 == *"$string2"* ]]; then
echo "string1 contains string2"
elif [[ $string2 == *"$string1"* ]]; then
echo "string2 contains string1"
fi
Within [[...]] the == operator is a pattern matching operator.
Ref: 6.4 Bash Conditional Expressions
For specifically an optional "a" or "the" prefix:
[[ $string1 == ?(a|the)"$string2" || $string2 == ?(a|the)"$string1" ]]
That uses bash's extended patterns, see 3.5.8.1 Pattern Matching
We have some scripts that do things like
e=$?
if [[ $e == 123 ]]; then exit 1; fi
They're more complicated than that, it's just an example. My question is using double brackets acceptable to make numerical comparisons this way, is there any disadvantage? I would think it should be double parentheses if (( $e == 123 )) but I don't want to go changing a lot of scripts over nothing.
Thanks
There are a lot of key differences doing it, because == checks for exact string equality, but -eq evaluates both expressions arithmetically before checking for equality.
$ [[ " 1 " -eq 1 ]] && echo equal || echo not
equal
$ (( " 1 " == 1 )) && echo equal || echo not
equal
$ [[ " 1 " = 1 ]] && echo equal || echo not
not
Also, the empty string happens to be numerically equal to zero:
$ [[ "" -eq 0 ]] && echo equal || echo not
equal
$ [[ "" == 0 ]] && echo equal || echo not
not
And a whole other class of differences appears when you bring the comparison operators in - considering < vs -lt, for instance:
$ [[ 2 -lt 10 ]] && echo less || echo not
less
$ (( 2 < 10 )) && echo less || echo not
less
$ [[ 2 < 10 ]] && echo less || echo not
not
This is because the string 2 is alphabetically after the string 10 (since 1 comes before 2), but the number 2 is numerically less than the number 10.
Credits to the original cross site duplicate, with a few updates Is there any major difference when comparing a variable as a string or as an int?
The verdict is to use $((..)) for arithmetic comparisons strictly to avoid interpreting the operands as strings.
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
read A
if [["$A" == 'Y' -o "$A" =='y']]
then echo "YES"
else echo "NO"
fi
I am very new to shell scripting.basically, i am trying to check if the input is Y or y.
I am getting the following error which I am not able to debug.
solution.sh: line 2: [[Y: command not found
Thanks.
You are missing space after [[
The if should be like
if [[ "$A" == 'Y' || "$A" == 'y' ]]
The [[ ]] is an extended test command, like any command it should be separated from others by spaces
Add proper space inside the square brackets and after the ==:
read A
if [ "$A" == 'Y' -o "$A" == 'y' ]; then
echo "YES";
else
echo "NO";
fi
Note that to use -o you ought to use test command, which uses single brackets.
If you use [[ ]], with double brackets, you must use || instead of -o:
read A
if [[ "$A" == 'Y' || "$A" == 'y' ]]; then
echo "YES";
else
echo "NO";
fi
I am trying to compare some characters in BASH
read a
if (($a == "Y"))
then
echo "YES"
elif (($a == "y"))
then
echo "YES"
else
echo "NO"
fi
but for some reason it does not work as expected. It always output YES. Looking at this super simple script, I can not understand what is wrong.
It's due to spacing and the brackets.
read a
if [[ $a == "Y" ]]
then
echo "YES"
elif [[ $a == "y" ]]
then
echo "YES"
else
echo "NO"
fi
You should review bash comparison. You're trying to use an arithmetic expansion construct to do string comparison. Instead, you should use the [ exp ] or [[ exp ]] constructs.
((expression)) is used for 'arithmetic evaluation' and strings inside (( )) will be treated as variable names, thus
if (($a == "Y"))
is equivalent to
if [ $a == $Y ]
(if $Y is a string, then bash will try to expand the name until it finds a numeric value or undefined variable)
You need to use [ ] or [[ ]] to compare strings as #KRUKUSA said.
This question already has answers here:
Simple logical operators in Bash
(5 answers)
Closed 7 years ago.
I would like to have the logical not for the following condition expression in bash,
how can I do this?
if [[ $var==2 || $var==30 || $var==50 ]] ; then
do something
fi
how can I prepend the logical not directly in the above expression, it's very tedious to change it again into things like this:
if [[ $var!=2 && $var!=30 && $var==50 ]] ; then
do something
fi
thanks for any hints!
if ! [[ $var == 2 || $var == 30 || $var == 50 ]] ; then
do something
fi
Or:
if [[ ! ($var == 2 || $var == 30 || $var == 50) ]] ; then
do something
fi
And a good practice is to have spaces between your conditional operators and operands.
Some could also suggest that if you're just comparing numbers, use an arithmetic operator instead, or just use (( )):
if ! [[ var -eq 2 || var -eq 30 || var -eq 50 ]] ; then
do something
fi
if ! (( var == 2 || var == 30 || var == 50 )) ; then
do something
fi
Although it's not commendable or caution is to be given if $var could sometimes be not numeric or has no value or unset, since it could mean 0 as default or another value of another variable if it's a name of a variable.