4

I am missing something fundamental concerning either the bash's if construct/operators or string comparison. 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

So it enters each case even though they should be mututally exclusive. Any help is appreciated.

1
  • 1
    I suggest using '=' instead of '==' for POSIX conformance Commented Jun 14, 2013 at 7:30

1 Answer 1

8

bash happens to be somewhat particular about spaces.

Add spaces around the operators:

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

...

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

The following are not equivalent:

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.