I've come across this piece of code from here:
#!/bin/bash
# ...
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
echo "Usage..."
exit
fi
I understand what this does is print the usage information when any argument like -h
, --help
, -help
, -------help
is passed.
But why "${1-}"
and not just "${1}"
? What does the hyphen do?
At first I thought it could mean any argument from the position 1 onward. I tried it and no, it wasn't that.
Then I thought it could be a typo and the author meant "${1-9}"
to allow all number positions. Again, it didn't work, it isn't even valid code.