In bash 3.2 (default on the recent macOS), running printf "-1"
gives me some error like this:
bash: printf: -1: invalid option
Which I have no problem in zsh
, indeed, a leading hyphen in any string passed to printf
will trigger that error. shellcheck
has not warning for this. Also tried echo "-1"
and seems fine.
I know the error is avoidable by using printf "%s" "-1"
, but can someone explain the reason behind it? thanks.
For utilities that conform to the standard syntax (echo
being a notable exception), leading operands starting with -
indicate options. Operands are treated as options even if the command does not provide options by those names, causing the error message you've seen. An explicit end of the options can be indicated with --
: printf -- -1
will cause the string -1
to be interpreted as the format string and printed.
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