koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.36k stars 1.77k forks source link

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

Open mcandre opened 2 years ago

mcandre commented 2 years ago

For new checks and feature suggestions

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.

brother commented 2 years ago

Isn't the current warning enough?

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

brother commented 2 years ago

(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). )