]> git.ipfire.org Git - thirdparty/u-boot.git/commit
cmd: test: add bug-compatibility special case for 'test -n'
authorRasmus Villemoes <ravi@prevas.dk>
Mon, 30 Mar 2026 14:01:06 +0000 (16:01 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 2 Apr 2026 22:00:29 +0000 (16:00 -0600)
commitf7e7c55e53e80100c327b9cb0512c069acf80ab5
tree76026da1264f12757d7bee3a0cb46ae48a0b278e
parentd1cd6733917fa67c262fbad2520da93d788e17f7
cmd: test: add bug-compatibility special case for 'test -n'

It turns out that there is lots of code in the wild, including in the
U-Boot tree itself, which used to rely on

  test -n $somevar

to yield false when $somevar is not defined or empty. See for example
all the occurrences of 'test -n $fdtfile'. That was really only a
quirk of the implementation that refused calls with argc < 3, and not
because it was interpreted as

  test -n "$somevar"

which is how this should be spelled.

While not exactly conforming to POSIX, we can accomodate such scripts
by special-casing a single argument "-n" to be interpreted as if it
comes from code as above with empty $somevar.

Since we only just added the ability to test a string for emptiness
using the single-argument form, it is very unlikely that there is code
doing

  test "$str"

which would now fail if $str happens to be exactly "-n"; such a test
should really always be spelled

  test -n "$str"

Fixes: 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
Reported-by: Franz Schnyder <franz.schnyder@toradex.com>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>
cmd/test.c
test/hush/if.c