I wonder why the test [[ ! -v 1 ]]
fails no matter if I pass the 1st positional parameter to the function below:
shopt -os nounset
function foo {
echo -n "$FUNCNAME: 1st positional parameter "
[[ ! -v 1 ]] && echo "is missing." || echo is "\"$1\"."
}
I know there are other ways to test but why doesn't this particular test work?
In this case, you want to check if the parameter is unset.
has_1() {
if [[ -z "${1+present}" ]]; then
echo "no first param"
else
echo "given: $1"
fi
}
The parameter expansion ${var+word}
will return "word" only if the parameter is not unset -- i.e. if you pass an empty string, the function will indicate the first parameter is given.
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