cmd: test: allow using [ as alias for test

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>
Tested-by: Anshul Dalal <anshuld@ti.com>
This commit is contained in:
Rasmus Villemoes
2026-03-12 11:01:03 +01:00
committed by Tom Rini
parent 12a9c83cba
commit fc8bf9a984

View File

@@ -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[])
{