Comparing Variables with Strings Bash

How do I compare two string variables in an 'if' statement in Bash?

For string equality comparison, use:

if [[ "$s1" == "$s2" ]]

For string does NOT equal comparison, use:

if [[ "$s1" != "$s2" ]]

For the a contains b, use:

if [[ $s1 == *"$s2"* ]]

(and make sure to add spaces between the symbols):

Bad:

if [["$s1" == "$s2"]]

Good:

if [[ "$s1" == "$s2" ]]

Comparing variables with strings bash

You need whitespace around the = sign; without it, the shell treats the "expression" as a single word, not a comparison of two words for equality.

[[ "$version" = "CentOS release 6.9 (Final)" ]]

[[ a=b ]] is equivalent to [[ -n a=b ]], i.e., you are simply testing if the single word is a non-empty string.

Whether you use = or == inside [[ ... ]] is a matter of style; bash supports both.

I would avoid using a versionknown variable altogether, and write the code like this:

version=$(</etc/centos-release)
known_version1="CentOS release 6.9 (Final)"
known_version2="CentOS Linux release 7.3.1611 (Core)"

if [[ "$version" = "$known_version1" ]] ; then
echo "Same versions as expected"
#run commands for this specific version
elif [[ "$version" = "$known_version2" ]] ; then
echo "Same versions as expected v2"
#run commands for this specific version
else
echo "Different version detected than known:"
echo "$version"
echo "Aborted"
fi

or a case statement

case $version in
"$known_version1")
echo "Same versions as expected"
# ...
;;
"$known_version2")
echo "Same versions as expected v2"
# ...
;;
*)
echo "Different version detected than known:"
echo "$version"
echo "Aborted"
esac

How to compare strings in Bash

Using variables in if statements

if [ "$x" = "valid" ]; then
echo "x has the value 'valid'"
fi

If you want to do something when they don't match, replace = with !=. You can read more about string operations and arithmetic operations in their respective documentation.

Why do we use quotes around $x?

You want the quotes around $x, because if it is empty, your Bash script encounters a syntax error as seen below:

if [ = "valid" ]; then

Non-standard use of == operator

Note that Bash allows == to be used for equality with [, but this is not standard.

Use either the first case wherein the quotes around $x are optional:

if [[ "$x" == "valid" ]]; then

or use the second case:

if [ "$x" = "valid" ]; then

Compare String with list of strings in bash

Assuming the az ml command returns a json array string and you want to
check if the array includes the value of variable SERVNAME, would you
please try:

SERVNAME="ner"
SERVICE='[ "ner", "aks-gpu-ner-0306210907", "aks-gpu-ner-30012231", "aks-gpu-ner-1305211336"]'

if [[ $SERVICE =~ "\"$SERVNAME\"" ]]; then
echo "Service Found"
# put your command here to update the service
else
echo "Service Not Found"
# put your command here to deploy new service
fi

The regex operator $SERVICE =~ "\"$SERVNAME\"" matches if the string $SERVICE
contains the substring $SERVNAME enclosed with double quotes.

If jq is available, you could also say:

result=$(echo "$SERVICE" | jq --arg var "$SERVNAME" '. | index($var)')
if [[ $result != "null" ]]; then
echo "Service Found"
else
echo "Service Not Found"
fi

Comparing two string variables in bash

The condition works based on the number of elements within it, and this particular issue is covered by this (paraphrased) part of the man-page:

string: True if the length of string is non-zero.

string1 == string2: True if the strings are equal.

In other words, a comparison needs three elements, meaning you need a space on either side of the ==.

Without that it's simply the one-element variant of the condition, which is true when the string is non-empty, as hello==h most definitely is.

How compare two string variables to two string and combine them in AND logic operator in IF conditional statement

Use bash rematch:

re='\+8'
[[ "$lossPerc" =~ $re ]] && [[ "$prevLostPerc" =~ $re ]]

Or even like this:

re='\+8.*\+8'
[[ "$lossPerc$prevLostPerc" =~ $re ]]

If case is also an option:

case "$lossPerc$prevLostPerc" in
*'+8'*'+8'*) echo ok;;
esac

Bash compare if two strings match by length

if [[ ${#string} != ${#string_two} ]]; then
run command
fi


Related Topics



Leave a reply



Submit