It's not a bug!
The command substitution (driver X)
executes the driver function, and then turns each output line into an argument. In the case of (driver 0), there's no output, so you get zero arguments. So the no output case is equivalent to running test -z
and test -n
.
Good old IEEE 1003.1 tells us what test must do in this case:
1 argument: Exit true(0) if $1 is not null; otherwise, exit false
So when -n is the only argument, it loses its status as a flag, and you just end up testing '-n' for non-nullity (which of course it passes).
You can see the same behavior in bash:
> test -n `echo -n` ; echo $?
0
In fish, if you want to check if a string is non-empty, you can use count
:
if count (driver 0) > /dev/null
# output!
end
You can also use an intermediate variable with test:
set -l tmp (driver 0)
test -n "$tmp" ; and echo some output; or echo no output
The quotes ensures that $tmp always becomes one argument (possibly empty).