bug#79901: Inconsistency between '-n STRING' and 'STRING' in TEST

Carlos Maiolino via GNU coreutils Bug Reports <[email protected]> Thu, 27 Nov 2025 13:33:06 +0100
Newsgroups gmane.comp.gnu.core-utils.bugs
Message-ID <tzymi3atyshchzzutfn4jx7mevcp3sf4isxsfxuw77ajlclzrj@42qkand5ixgw>
Hello,

I'm not sure if the behavior below is expected or perhaps a bug?
I'm using GNU coreutils 9.6

According to the TEST(1) manpage:

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING


However, the small script below will produce inconsistent results, where
only the first if conditional will evaluate to True:

#!/bin/bash

B=" "

if [ -n $B ]; then
        echo "passed 1"
fi

if [ $B ]; then
        echo "passed 2"
fi


However it seems to me the variable expansion plays a role here, I'm not
sure if the variable expansion was supposed to differ between "$B" and
"-n $B" too, so perhaps it's a bash thing not a "test" thing?

Cheers,
Carlos