kusalananda / shell-toolbox

Useful shell scripts for /bin/sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Disabling SC2059 in shell.in

0mp opened this issue · comments

# We mimic printf here, disable shellcheck test.
# shellcheck disable=SC2059
printf "$@" >&2

Are you sure this is what you want?

Isn't printf "%s" "$*" >&2 just fine here?

😄 Cheers!

No, not really. I would want to be able to use the function as a drop-in replacement for printf to do error output. This means that it must be able to take a printf format string followed by a number of arguments.

In my current code, which I haven't pushed just yet, this looks like

# We mimic printf here, disable shellcheck warnings.
# See https://unix.stackexchange.com/questions/438694
# shellcheck disable=SC2059,SC2145
printf "$UTIL: $@" >&2

The two disabled warnings are:

  • SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".
  • SC2145: Argument mixes string and array. Use * or separate argument.

I asked a question on U&L about this (see link in above code) and Stéphane Chazelas answered it and gave the above as an alternative solution, together with caveats for the $UTIL string (will be set to shell here, so no issues there).

Alright, it does make sense then. Thank you for your explanation 👍