shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2142: false alarm about positional parameters of external shell script inside an alias

Open GfEW opened this issue 6 months ago • 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.

GfEW avatar Jul 10 '25 20:07 GfEW

What was this code supposed to do?

~ $ set -- -n:-e; alias + set -- -n:-e

  • alias alias expandtocolons='bash -cvx '''IFS=:; echo "${}"''' --' ~ $ expandtocolons quux + bash -cvx 'IFS=:; echo "${}"' -- quux IFS=:; echo "${*}"
  • IFS=:
  • echo quux quux ~ $ expandtocolons quux:bla
  • bash -cvx 'IFS=:; echo "${}"' -- quux:bla IFS=:; echo "${}"
  • IFS=:
  • echo quux:bla quux:bla

On Thu, Jul 10, 2025, 13:27 GfEW @.***> wrote:

GfEW created an issue (koalaman/shellcheck#3245) https://github.com/koalaman/shellcheck/issues/3245 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 "${*}"''' --' ^-- SC2142 (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?

Shellcheck should not throw the error. Additional remarks

Whilst that specific alias could easily be defined as a plain function instead of an alias that calls an external shell, the main point of the MRE is to technically demonstrate the issue.

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3245, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F26ZE3E4OVP5NA5XZX33H3EEFAVCNFSM6AAAAACBIBCBAOVHI2DSMVQWIX3LMV43ASLTON2WKOZTGIZDANRQGE2DSNA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

wileyhy avatar Jul 10 '25 22:07 wileyhy

@wileyhy

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.

GfEW avatar Jul 12 '25 00:07 GfEW

Oh, okay. Here's a workaround for that.

#!/bin/bash bar=( /lib/{R,cargo,dx}/bin ) OLDIFS=$IFS IFS=: printf '<%s>\n' "${bar[*]}" IFS=$OLDIFS

On Fri, Jul 11, 2025, 17:49 GfEW @.***> wrote:

GfEW left a comment (koalaman/shellcheck#3245) https://github.com/koalaman/shellcheck/issues/3245#issuecomment-3064439868

@wileyhy https://github.com/wileyhy

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, it's mainly about the false positive in SC2142.

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3245#issuecomment-3064439868, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F2YTGCVCQGF2J2DZY4T3IBLSJAVCNFSM6AAAAACBIBCBAOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTANRUGQZTSOBWHA . You are receiving this because you were mentioned.Message ID: @.***>

wileyhy avatar Jul 12 '25 01:07 wileyhy

Oh, okay. Here's a workaround for that.

Thanks for the suggestion, but

  1. this issue strives to fix SC2142, not expandtocolons
  2. I've created expandtocolons specifically for this report, rather than the other way around
  3. 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.

GfEW avatar Jul 12 '25 14:07 GfEW