* [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling
@ 2026-03-12 10:01 Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 1/4] cmd: test: allow using [ as alias for test Rasmus Villemoes
` (5 more replies)
0 siblings, 6 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-12 10:01 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Quentin Schulz, Rasmus Villemoes
Make 'test' behave a little more like its cousins in other shells, by
allowing the [ ... ] spelling, and while here, fix up the handling of
a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
v1: https://lore.kernel.org/u-boot/20260311120910.3934114-1-ravi@prevas.dk/
Rasmus Villemoes (4):
cmd: test: allow using [ as alias for test
doc: test: document [ ] spelling of test
test: add tests for left-bracket alias for 'test' command
cmd: test: fix handling of single-argument form of test
cmd/test.c | 30 +++++++++++++++--
doc/usage/cmd/test.rst | 5 ++-
test/hush/if.c | 73 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 105 insertions(+), 3 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/4] cmd: test: allow using [ as alias for test
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
@ 2026-03-12 10:01 ` Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 2/4] doc: test: document [ ] spelling of test Rasmus Villemoes
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-12 10:01 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Quentin Schulz, Rasmus Villemoes
I often find myself writing something like
if [ "$somevar" = 123 ] ; then ...
only to realize that that syntax doesn't work in U-Boot shell, and
must be spelled
if test "$somevar" = 123 ; then
It only takes a few lines of code to support this POSIX-standardized
alias for test, and helps developers focus on their actual problems
instead of dealing with such unexpected quirks of the shell.
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
cmd/test.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/cmd/test.c b/cmd/test.c
index a9ac07e6143..f0645ef98f1 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -63,6 +63,14 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
char * const *ap;
int i, op, left, adv, expr, last_expr, last_unop, last_binop;
+ if (!strcmp(argv[0], "[")) {
+ if (strcmp(argv[argc - 1], "]")) {
+ printf("[: missing terminating ]\n");
+ return 1;
+ }
+ argc--;
+ }
+
/* args? */
if (argc < 3)
return 1;
@@ -212,6 +220,17 @@ U_BOOT_CMD(
"[args..]"
);
+/*
+ * This does not use the U_BOOT_CMD macro as [ can't be used in symbol names
+ */
+ll_entry_declare(struct cmd_tbl, lbracket, cmd) = {
+ "[", CONFIG_SYS_MAXARGS, cmd_always_repeatable, do_test,
+ "alias for 'test'",
+#ifdef CONFIG_SYS_LONGHELP
+ " <test expression> ]"
+#endif /* CONFIG_SYS_LONGHELP */
+};
+
static int do_false(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/4] doc: test: document [ ] spelling of test
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 1/4] cmd: test: allow using [ as alias for test Rasmus Villemoes
@ 2026-03-12 10:01 ` Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 3/4] test: add tests for left-bracket alias for 'test' command Rasmus Villemoes
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-12 10:01 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Quentin Schulz, Rasmus Villemoes
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
doc/usage/cmd/test.rst | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/doc/usage/cmd/test.rst b/doc/usage/cmd/test.rst
index d1379117fca..037a9ee1774 100644
--- a/doc/usage/cmd/test.rst
+++ b/doc/usage/cmd/test.rst
@@ -20,11 +20,14 @@ Synopsis
test -e <interface> <dev[:part]> <path>
test <s> =~ <re>
+ [ <test expression> ]
+
Description
-----------
The ``test`` command is similar to the ordinary shell built-in by the
-same name. Unlike in ordinary shells, it cannot be spelled ``[``.
+same name. Like in ordinary shells, it can also be spelled ``[``,
+provided the test expression is followed by a separate ``]`` argument.
Strings
~~~~~~~
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/4] test: add tests for left-bracket alias for 'test' command
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 1/4] cmd: test: allow using [ as alias for test Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 2/4] doc: test: document [ ] spelling of test Rasmus Villemoes
@ 2026-03-12 10:01 ` Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 4/4] cmd: test: fix handling of single-argument form of test Rasmus Villemoes
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-12 10:01 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Quentin Schulz, Rasmus Villemoes
Duplicate a few of the existing test cases, using the [ spelling, and
also ensure that the presence of a matching ] as a separate and last
argument is enforced.
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
test/hush/if.c | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/test/hush/if.c b/test/hush/if.c
index ea615b246a9..5f75ea68b32 100644
--- a/test/hush/if.c
+++ b/test/hush/if.c
@@ -315,3 +315,46 @@ static int hush_test_if_z_operator(struct unit_test_state *uts)
return 0;
}
HUSH_TEST(hush_test_if_z_operator, 0);
+
+static int hush_test_lbracket_alias(struct unit_test_state *uts)
+{
+ char if_formatted[128];
+ const char *missing_rbracket_error = "[: missing terminating ]";
+
+ sprintf(if_formatted, if_format, "[ aaa = aaa ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ aaa = bbb ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ aaa = aaa");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ aaa = bbb");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ aaa = aaa]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ aaa = bbb]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ aaa != aaa -o bbb != bbb ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ aaa != aaa -o bbb = bbb ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb != bbb ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb = bbb ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ return 0;
+}
+HUSH_TEST(hush_test_lbracket_alias, UTF_CONSOLE);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/4] cmd: test: fix handling of single-argument form of test
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
` (2 preceding siblings ...)
2026-03-12 10:01 ` [PATCH v2 3/4] test: add tests for left-bracket alias for 'test' command Rasmus Villemoes
@ 2026-03-12 10:01 ` Rasmus Villemoes
2026-03-13 6:24 ` [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Anshul Dalal
2026-03-25 23:05 ` Tom Rini
5 siblings, 0 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-12 10:01 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini, Quentin Schulz, Rasmus Villemoes
POSIX states that
0 arguments:
Exit false (1).
1 argument:
Exit true (0) if $1 is not null; otherwise, exit false.
and at least bash and busybox sh behave that way.
The current 'argc < 3' does the right thing for a non-existing or
empty argv[1], but not for a non-empty argv[1]. Fix that and add
corresponding test cases.
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
cmd/test.c | 11 +++++++++--
test/hush/if.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/cmd/test.c b/cmd/test.c
index f0645ef98f1..0d0f090386c 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -71,10 +71,17 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
argc--;
}
- /* args? */
- if (argc < 3)
+ /*
+ * Per POSIX, 'test' with 0 arguments should return 1, while
+ * 'test <arg>' should be equivalent to 'test -n <arg>',
+ * i.e. true if and only if <arg> is not empty.
+ */
+ if (argc < 2)
return 1;
+ if (argc == 2)
+ return !strcmp(argv[1], "");
+
#ifdef DEBUG
{
debug("test(%d):", argc);
diff --git a/test/hush/if.c b/test/hush/if.c
index 5f75ea68b32..6129e2c530c 100644
--- a/test/hush/if.c
+++ b/test/hush/if.c
@@ -32,6 +32,15 @@ static int hush_test_if_base(struct unit_test_state *uts)
sprintf(if_formatted, if_format, "false");
ut_asserteq(1, run_command(if_formatted, 0));
+ sprintf(if_formatted, if_format, "test");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "test ''");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "test 'abc'");
+ ut_assertok(run_command(if_formatted, 0));
+
return 0;
}
HUSH_TEST(hush_test_if_base, 0);
@@ -355,6 +364,27 @@ static int hush_test_lbracket_alias(struct unit_test_state *uts)
sprintf(if_formatted, if_format, "[ ! aaa != aaa -o ! bbb = bbb ]");
ut_assertok(run_command(if_formatted, 0));
+ sprintf(if_formatted, if_format, "[ ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ '' ]");
+ ut_asserteq(1, run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ ''");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
+ sprintf(if_formatted, if_format, "[ 'abc' ]");
+ ut_assertok(run_command(if_formatted, 0));
+
+ sprintf(if_formatted, if_format, "[ 'abc'");
+ ut_asserteq(1, run_command(if_formatted, 0));
+ ut_assert_nextline(missing_rbracket_error);
+
return 0;
}
HUSH_TEST(hush_test_lbracket_alias, UTF_CONSOLE);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
` (3 preceding siblings ...)
2026-03-12 10:01 ` [PATCH v2 4/4] cmd: test: fix handling of single-argument form of test Rasmus Villemoes
@ 2026-03-13 6:24 ` Anshul Dalal
2026-03-25 23:05 ` Tom Rini
5 siblings, 0 replies; 9+ messages in thread
From: Anshul Dalal @ 2026-03-13 6:24 UTC (permalink / raw)
To: Rasmus Villemoes, u-boot; +Cc: Tom Rini, Quentin Schulz
On Thu Mar 12, 2026 at 3:31 PM IST, Rasmus Villemoes wrote:
> Make 'test' behave a little more like its cousins in other shells, by
> allowing the [ ... ] spelling, and while here, fix up the handling of
> a single, non-empty argument to comply with POSIX.
>
> v2: update documentation, add a few more test cases.
>
> CI is happy: https://github.com/u-boot/u-boot/pull/905
>
> v1: https://lore.kernel.org/u-boot/20260311120910.3934114-1-ravi@prevas.dk/
Tested-by: Anshul Dalal <anshuld@ti.com>
>
> Rasmus Villemoes (4):
> cmd: test: allow using [ as alias for test
> doc: test: document [ ] spelling of test
> test: add tests for left-bracket alias for 'test' command
> cmd: test: fix handling of single-argument form of test
>
> cmd/test.c | 30 +++++++++++++++--
> doc/usage/cmd/test.rst | 5 ++-
> test/hush/if.c | 73 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 105 insertions(+), 3 deletions(-)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
` (4 preceding siblings ...)
2026-03-13 6:24 ` [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Anshul Dalal
@ 2026-03-25 23:05 ` Tom Rini
2026-03-27 15:48 ` Franz Schnyder
5 siblings, 1 reply; 9+ messages in thread
From: Tom Rini @ 2026-03-25 23:05 UTC (permalink / raw)
To: u-boot, Rasmus Villemoes; +Cc: Quentin Schulz
On Thu, 12 Mar 2026 11:01:02 +0100, Rasmus Villemoes wrote:
> Make 'test' behave a little more like its cousins in other shells, by
> allowing the [ ... ] spelling, and while here, fix up the handling of
> a single, non-empty argument to comply with POSIX.
>
> v2: update documentation, add a few more test cases.
>
> CI is happy: https://github.com/u-boot/u-boot/pull/905
>
> [...]
Applied to u-boot/next, thanks!
[1/4] cmd: test: allow using [ as alias for test
commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588
[2/4] doc: test: document [ ] spelling of test
commit: d44f61582947564991cce7501f2e5db0b29ebad6
[3/4] test: add tests for left-bracket alias for 'test' command
commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1
[4/4] cmd: test: fix handling of single-argument form of test
commit: 8b0619579b2282050e7fb0d92fbc645b79d18bae
--
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling
2026-03-25 23:05 ` Tom Rini
@ 2026-03-27 15:48 ` Franz Schnyder
2026-03-27 19:38 ` Rasmus Villemoes
0 siblings, 1 reply; 9+ messages in thread
From: Franz Schnyder @ 2026-03-27 15:48 UTC (permalink / raw)
To: Tom Rini
Cc: u-boot, Rasmus Villemoes, Quentin Schulz, Francesco Dolcini,
Franz Schnyder
On Wed, Mar 25, 2026 at 05:05:59PM -0600, Tom Rini wrote:
> On Thu, 12 Mar 2026 11:01:02 +0100, Rasmus Villemoes wrote:
>
> > Make 'test' behave a little more like its cousins in other shells, by
> > allowing the [ ... ] spelling, and while here, fix up the handling of
> > a single, non-empty argument to comply with POSIX.
> >
> > v2: update documentation, add a few more test cases.
> >
> > CI is happy: https://github.com/u-boot/u-boot/pull/905
> >
> > [...]
>
> Applied to u-boot/next, thanks!
>
> [1/4] cmd: test: allow using [ as alias for test
> commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588
> [2/4] doc: test: document [ ] spelling of test
> commit: d44f61582947564991cce7501f2e5db0b29ebad6
> [3/4] test: add tests for left-bracket alias for 'test' command
> commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1
> [4/4] cmd: test: fix handling of single-argument form of test
> commit: 8b0619579b2282050e7fb0d92fbc645b79d18bae
> --
> Tom
>
>
Hello,
While preparing a patch series based on u-boot/next, I realised that
the bootflow on our module broke.
I discovered that the issue was introduced with
commit 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
This change breaks our boot script as it relies on the behaviour of test
with unquoted empty expansions. For example:
```
test -n ${m4boot} || env set m4boot ';'
```
Is now `test -n`, as there are no quotation marks. It now returns
true, so the fallback assignment is skipped and it breaks our boot flow.
Any ideas? I think that we will not be the only ones affected by
this once it's merged into master.
Best regards
Franz Schnyder
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling
2026-03-27 15:48 ` Franz Schnyder
@ 2026-03-27 19:38 ` Rasmus Villemoes
0 siblings, 0 replies; 9+ messages in thread
From: Rasmus Villemoes @ 2026-03-27 19:38 UTC (permalink / raw)
To: Franz Schnyder
Cc: Tom Rini, u-boot, Quentin Schulz, Francesco Dolcini,
Franz Schnyder
On Fri, Mar 27 2026, Franz Schnyder <fra.schnyder@gmail.com> wrote:
> On Wed, Mar 25, 2026 at 05:05:59PM -0600, Tom Rini wrote:
>> On Thu, 12 Mar 2026 11:01:02 +0100, Rasmus Villemoes wrote:
>>
>> > Make 'test' behave a little more like its cousins in other shells, by
>> > allowing the [ ... ] spelling, and while here, fix up the handling of
>> > a single, non-empty argument to comply with POSIX.
>> >
>> > v2: update documentation, add a few more test cases.
>> >
>> > CI is happy: https://github.com/u-boot/u-boot/pull/905
>> >
>> > [...]
>>
>> Applied to u-boot/next, thanks!
>>
>> [1/4] cmd: test: allow using [ as alias for test
>> commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588
>> [2/4] doc: test: document [ ] spelling of test
>> commit: d44f61582947564991cce7501f2e5db0b29ebad6
>> [3/4] test: add tests for left-bracket alias for 'test' command
>> commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1
>> [4/4] cmd: test: fix handling of single-argument form of test
>> commit: 8b0619579b2282050e7fb0d92fbc645b79d18bae
>> --
>> Tom
>>
>>
> Hello,
>
> While preparing a patch series based on u-boot/next, I realised that
> the bootflow on our module broke.
> I discovered that the issue was introduced with
> commit 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
>
> This change breaks our boot script as it relies on the behaviour of test
> with unquoted empty expansions. For example:
>
> ```
> test -n ${m4boot} || env set m4boot ';'
> ```
> Is now `test -n`, as there are no quotation marks. It now returns
> true, so the fallback assignment is skipped and it breaks our boot flow.
>
> Any ideas? I think that we will not be the only ones affected by
> this once it's merged into master.
Urrgh, sorry about that. That snippet is arguably fragile and buggy, but
yes, similar code can exist elsewhere.
I don't really like it, but I suppose one could special-case the
single-argument case and interpret
test -n
as
test -n ""
with all other single-argument strings behaving as they do now. This
would need a separate test case and explicit documentation. It's
probably quite rare that one would test a string for emptiness where a
possible non-empty value is exactly "-n", and since the single-argument
case hasn't really been supported in U-Boot until now, I don't think
such code really exists. Moreover, I don't think one should ever rely on
that single-argument behaviour anyway;
test "$str"
should always be spelled
test -n "$str"
Single-argument 'test -z' does not need special treatment, because
both 'test -z' and 'test -z ""' are true, but we could add the former as
a test case as well if we do implement the above.
Rasmus
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-03-27 19:38 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 10:01 [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 1/4] cmd: test: allow using [ as alias for test Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 2/4] doc: test: document [ ] spelling of test Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 3/4] test: add tests for left-bracket alias for 'test' command Rasmus Villemoes
2026-03-12 10:01 ` [PATCH v2 4/4] cmd: test: fix handling of single-argument form of test Rasmus Villemoes
2026-03-13 6:24 ` [PATCH v2 0/4] add [ as alias for test, fix 0/1 argument handling Anshul Dalal
2026-03-25 23:05 ` Tom Rini
2026-03-27 15:48 ` Franz Schnyder
2026-03-27 19:38 ` Rasmus Villemoes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox