mike628
mike628

Reputation: 49401

Why would bash string comparison stop equating to true

I have a java config file with the following entry

Success=true

All of a sudden this comparison does not seem to equate to Boolean true .

if [ "$Success" == "true" ];then
//do something
fi

I've checked for Windows line endings and spaces. This is bash 5.0.17, which is fairly new. Its running on Solaris. Seems simple enough and we don't want to edit the script if at all possible. What can be other explanations ?

Upvotes: 0

Views: 61

Answers (1)

maximus
maximus

Reputation: 746

Using == for comparison is allowed but not standard.

  • Use the = operator with the test [ command.
  • Use the == operator with the [[ command for pattern matching.

So the fix could like that:

if [ "$Success" = "true" ]; then
//do something
fi

or

if [[ "$Success" == "true" ]]; then
  //do something
fi

Upvotes: 1

Related Questions