shellcheck
shellcheck copied to clipboard
SC3045: incomplete wiki page when printing a literal '-'
For bugs
- Rule Id (if any, e.g. SC1000): SC3045
- My shellcheck version (
shellcheck --versionor "online"):online - [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
- [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit
For new checks and feature suggestions
- [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
- [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related
Here's a snippet or screenshot that shows the problem:
#!/bin/sh
printf '-> %s\n' "$1"
Here's what shellcheck currently says:
[Line 3:]
printf '-> %s\n' "$1"
^-- SC3045(warning): In POSIX sh, printf -> is undefined.
Here's what I wanted or expected to see:
The problem is that the wiki page is missing information on this case. In the example I am printing a literal - instead of passing an undefined flag to printf as explained in the wiki page.
However the example is still wrong because some shell implementations may actually support - flags for printf which will then become an undefined option when the printf format string starts with a -. The wiki could add information that the script could use the ascii code \055 instead when printing a literal -. The following script works and shows no warnings with shellcheck.net.
#!/bin/sh
printf '\055> %s\n' "$1"
Right, the wiki page needs two more examples.
One where -- can reliably be used, e.g. grep "->foo" * → grep -- "->foo" * (though I’d probably use -e there myself), and one where it’s questionable, like in the printf example which is better served with encoding the hyphen-minus.