Skip to main content
added [[ to title as alias for `test`
Link
enharmonic
  • 732
  • 9
  • 15

Why does -n appear in my bash test [[ condition debug output?

Source Link
enharmonic
  • 732
  • 9
  • 15

Why does -n appear in my bash test condition debug output?

I would like to test if a condition is true or false. Here is a sample bash script:

#!/bin/bash
set -x
var=foo
if [[ $var==bar ]]
then
        echo $var is bar
else
        echo $var is not bar
fi
set +x

I expect that the output will be foo is not bar, but I get the following output instead: foo is bar

While viewing the debug output, I noticed that the condition was being evaluated with -n:

+ var=foo
+ [[ -n foo==bar ]]
+ echo foo is bar
foo is bar
+ set +x

On the Bash Conditional Expressions man page it says

-n string

True if the length of string is non-zero.

But I'm not interested in whether the string length is non-zero, but rather whether the two strings are equal to each other.