SC2142: false alarm about positional parameters of external shell script inside an alias
Opened this issue · 4 comments
The problem
Shellcheck throws a bogus error if positional parameters occur in script arguments to an external shell inside an alias, as demonstrated by this MRE:
$ cat <<'' | shellcheck -
#!/usr/bin/bash
alias expandtocolons='bash -c '\''IFS=:; echo "${*}"'\'' -- '
In - line 2:
alias expandtocolons='bash -c '\''IFS=:; echo "${*}"'\'' -- '
^-- SC[2142](tel:2142) (error): Aliases can't use positional parameters. Use a function.
For more information:
https://www.shellcheck.net/wiki/SC2142 -- Aliases can't use positional para...
What should happen instead?
SC2142 should check where the positional parameter belongs, and not throw an error if it occurs inside a function definition or external shell script within the alias definition.
Additional remarks
Whilst expandtocolon could easily be defined as a plain function instead of an alias that calls an external shell, the main point of it is to technically demonstrate the issue with SC2142.
What was this code supposed to do?
Dumb example:
$ foo="$(expandtocolons /lib/{R,cargo,dx}/bin)"; declare -p foo
declare -- foo="/lib/R/bin:/lib/cargo/bin:/lib/dx/bin"
As already stated though, it's mainly about the false positive in SC2142.
Oh, okay. Here's a workaround for that.
Thanks for the suggestion, but
- this issue strives to fix SC2142, not
expandtocolons - I've created
expandtocolonsspecifically for this report, rather than the other way around expandtocolons, FWIW, already works as it is.
That aside, to be usable like the alias, that workaround script would need to read arguments rather than hard code them.
However, if you
- have to turn such tiny little one-liners into larger functions or scripts for no technical benefit other than circumventing a bogus alarm by your shell checker, or
- have to use SC directives wherever an alias contains a function definition or external script that reads arguments,
I think the better solution is to improve the shell checker so it recognizes where positional arguments belong.