From: Carlos Maiolino
Subject: bug#79901: Inconsistency between '-n STRING' and 'STRING' in TEST
Date: Thu, 27 Nov 2025 13:33:06 +0100
>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:
The short answer is: You need to quote $B, like this:
if [ "$B" ]; then
echo "passed 2"
fi
Then it will work as expected. shellcheck will tell you that it is
(almost) always right to quote your variables, and this is certainly
an instance where shellcheck is right.
BTW, purists will maintain that you should use the "-n" form only and
not the "just STRING" form; there seems to be some concern that in
pathological cases, the later form can fail. I've seen no evidence of
this. In particular, this works as expected:
foo=-x
[ "$foo" ] && echo "foo is not empty"
Note: All testing done using bash and, thus, the bash builtin version
of test/[. This thread should probably be moved to (reposted in)
one of the bash lists - unless, of course, you really are testing on
(and interested in) the coreutils version of test.
=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.
When responding to my posts, please try to refrain from giving bureaucratic
answers.
If you have nothing useful to say, then just click Next and go on.