shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Rule: Warn on bash-style array syntax for non-bash-family shells

Open mcandre opened this issue 3 years ago • 2 comments

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
echo $somearray[@]
echo $somearray[*]

These lines may be fine in bash, zsh, dash, posh, etc., but the lines are invalid in (POSIX) sh, ksh, and so on.

Here's what shellcheck currently says:

Here's what I wanted or expected to see:

Bash array syntax used, though a strict POSIX sh interpreter was detected.

mcandre avatar Sep 14 '22 22:09 mcandre

Isn't the current warning enough?

SC3054 (warning): In POSIX sh, array references are undefined.

brother avatar Sep 15 '22 08:09 brother

(you need to add the curly braces to get the error. SC1087 (error): Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet). )

brother avatar Sep 15 '22 08:09 brother