[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bash test builtin fails on: test -n '>' -a 1 -eq 1
From: |
cirrus . mazurka-0t |
Subject: |
Bash test builtin fails on: test -n '>' -a 1 -eq 1 |
Date: |
Tue, 22 Oct 2024 15:34:43 +0800 |
The following produces a `bash: test: too many arguments` exception in `test`,
with exit status 2:
``` bash
v='>'
test -n "$v" -a yes '!=' no # bash: test: too many arguments
echo $? # 2
test -n '>' -a 1 -eq 1 # bash: test: too many arguments
echo $? # 2
[ -n '>' -a 1 -eq 1 ] # bash: [: too many arguments
echo $? # 2
[[ -n '>' -a 1 -eq 1 ]]
# bash: syntax error in conditional expression
# bash: syntax error near `-a'
echo $? # 2
```
It works without the -a, and as such, it works using && instead of the -a:
```
v='>'
test -n "$v"
echo $? # 0
[ -n "$v" ]
echo $? # 0
test -n "$v" && test yes '!=' no
echo $? # 0
[ -n "$v" ] && [ yes '!=' no ]
echo $? # 0
```
There is no mention of this peculiar behaviour inside the documentation:
https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-test
Known versions affected:
GNU bash, version 5.2.37(1)-release (x86_64-apple-darwin22.6.0)
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin22)
If this is intended behaviour, then what is the suitable workaround?
1. Should I always use `&&` instead of `-a`?
2. Should I use a parameter replacement? e.g. `test -n "${v//>/.}" -a 1 -eq 1`
3. Is there another suggestion?
I've cross-posted this on Stack Exchange:
https://unix.stackexchange.com/q/785456/50703
Regards, Benjamin Lupton
- Bash test builtin fails on: test -n '>' -a 1 -eq 1,
cirrus . mazurka-0t <=