Bash if instruction behavior

I am missing something fundamental regarding bash if construct / operator or string comparisons. Consider the following script:

#!/bin/bash baseSystem="testdir1" testme="NA" if [ "$baseSystem"=="$testme" ]; then echo "In error case" fi if [ "$baseSystem"!="$testme" ]; then echo "In error case" fi 

I get:

 In error case In error case 

Thus, it is included in each case, even if they should be mutually exclusive. Any help is appreciated.

+4
source share
1 answer

bash is a bit special with respect to spaces.

Add spaces around the statements:

 if [ "$baseSystem" == "$testme" ]; then ... if [ "$baseSystem" != "$testme" ]; then 

The following are not :

 [ "$a"="$b" ] [ "$a" = "$b" ] 

Your first test is essentially the same as if [ "testdir1==NA" ]; then if [ "testdir1==NA" ]; then that would always be true.

+8
source

Source: https://habr.com/ru/post/1486185/


All Articles