bug#79901: Inconsistency between '-n STRING' and 'STRING' in TEST
Pádraig Brady <[email protected]> Thu, 27 Nov 2025 15:15:39 +0000
| Newsgroups | gmane.comp.gnu.core-utils.bugs |
|---|---|
| Message-ID | <[email protected]> |
tag 79901 notabug close 79901 stop details below... On 27/11/2025 12:33, Carlos Maiolino via GNU coreutils Bug Reports wrote: > 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? You need to quote the variable expansion to maintain spaces, otherwise `test -n $B` is equivalent to `test '-n'` which is true. You want: [ -n "$B" ] && [ "$B" ] && echo both passed cheers, Padraig.