I was wondering how comes
test -n
return 'true', for example :
if test -n; then echo "yes" ; else echo "no" ; fi
prints "yes", even though test was given, theoretically, an empty-length string as an argument along with the option -n, which checks whether the string length is 0 (returns false) or something else (returns true).
Thank you
From the documentation:
The
test
and[
builtins evaluate conditional expressions using a set of rules based on the number of arguments.0 arguments: The expression is false.
1 argument: The expression is true if and only if the argument is not null.
In your case you simply have one non-null argument (-n
).
It returns true for the same reason test x
returns true - the string -n
is non-empty. It is not exercising the -n
option because -n
requires a second argument and you've not provided one.
test -n "" || echo false
x=""
test -n $x && echo true
test -n "$x" || echo false
Each echo command is executed; note, in particular, the middle one!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With