shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Recognize as valid the usual workaround for Zsh word splitting on ${1+"$@"}

Open Julien-Elie opened this issue 2 years ago • 2 comments

For bugs

  • SC2142
  • 0.8.0
  • [x] The rule's wiki page does not already cover this
  • [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
# Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
# is contrary to our usage.  Disable this feature.
alias -g '${1+"$@"}'='"$@"'
setopt NO_GLOB_SUBST

Here's what shellcheck currently says:

In test line 4:
    alias -g '${1+"$@"}'='"$@"'
             ^----------------^ SC2142 (error): Aliases can't use positional parameters. Use a function.

Here's what I wanted or expected to see:

Couldn't it be a well-known syntax which does not emit an error? Libtool uses it in its wrappers (https://git.savannah.gnu.org/cgit/libtool.git/tree/build-aux/ltmain.in) and it is documented in the Autoconf manual (https://www.gnu.org/software/autoconf/manual/autoconf-2.71/html_node/Shell-Substitutions.html):

Zsh handles plain ‘"$@"’ properly, but we can’t use plain ‘"$@"’.
One workaround relies on Zsh’s “global aliases” to convert ‘${1+"$@"}’
into ‘"$@"’ by itself:

test ${ZSH_VERSION+y} && alias -g '${1+"$@"}'='"$@"'

Lots of projects use that syntax...

Julien-Elie avatar Feb 20 '22 08:02 Julien-Elie

Note that shellcheck doesn't support zsh (https://github.com/koalaman/shellcheck/issues/809) and most likely parses this script as a POSIX sh one. If you change the shebang to #!/bin/zsh, shellcheck will refuse to check the file. shellcheck will complain about a lot of zsh-only synax.

mineo avatar Apr 01 '22 12:04 mineo

I agree that shellcheck doesn't support zsh. My worry is that errors are triggered on all projects using Libtool. Such scripts are properly run by POSIX shells (like bash) and do not trigger any error when run. I was just asking whether an exception to SC2142 couldn't be added to take into account that well-known and wide-spread syntax.

Julien-Elie avatar Apr 02 '22 11:04 Julien-Elie