Open
Description
For bugs
- Rule Id: SC2178/SC2128
- My shellcheck version: online
Here's a snippet or screenshot that shows the problem:
if we start with this code:
#!/bin/bash
get_vars() {
local vars=( 1 2 3 )
echo "${vars[@]}"
}
main() {
local vars="$(get_vars)"
echo "${vars}"
}
we get:
$ shellcheck myscript
Line 7:
local vars="$(get_vars)"
^-- SC2155: Declare and assign separately to avoid masking return values.
^-- SC2178: Variable was used as an array but is now assigned a string.
Line 8:
echo "${vars}"
^-- SC2128: Expanding an array without an index only gives the first element.
but if we fix SC2155, all the other warnings disappear:
#!/bin/bash
get_vars() {
local vars=( 1 2 3 )
echo "${vars[@]}"
}
main() {
local vars
vars="$(get_vars)"
echo "${vars}"
}
$ shellcheck myscript
No issues detected!
if we remove the local
decl, they come back:
#!/bin/bash
get_vars() {
local vars=( 1 2 3 )
echo "${vars[@]}"
}
main() {
vars="$(get_vars)"
echo "${vars}"
}
$ shellcheck myscript
Line 7:
vars="$(get_vars)"
^-- SC2178: Variable was used as an array but is now assigned a string.
Line 8:
echo "${vars}"
^-- SC2128: Expanding an array without an index only gives the first element.
this is weird because the scope of var from main
to get_vars
hasn't changed ... the local var
in main
still cascades down into get_var
.
the SC2178/SC2128 should always be flagged even if the parent func declared it local
for its own usage.
Metadata
Metadata
Assignees
Labels
No labels