mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Restructure the "Exceptions" section; this is actually a bug in ShellCheck and should be clearly called out as such.
25
SC2128.md
25
SC2128.md
@@ -28,13 +28,14 @@ To get all elements as separate parameters, use the index `@` (and make sure to
|
|||||||
|
|
||||||
To get all elements as a single parameter, concatenated by the first character in `IFS`, use the index `*`. In the example, `echo "${myarray[*]}"` is equivalent to `echo "foo bar"`.
|
To get all elements as a single parameter, concatenated by the first character in `IFS`, use the index `*`. In the example, `echo "${myarray[*]}"` is equivalent to `echo "foo bar"`.
|
||||||
|
|
||||||
### Exceptions
|
### Bugs:
|
||||||
|
|
||||||
ShellCheck can get confused by variable scope if the same variable name was used as an array previously, but is a string in the current context. You can [[ignore]] it in this case.
|
There is a [known issue](https://github.com/koalaman/shellcheck/issues/1309) with this check's handling of `local` variables, causing ShellCheck to flag variables that were previously declared as arrays, even if they are in different scopes.
|
||||||
|
|
||||||
In the case of local variables, a workaround is to declare the local variable separately from assigning to it:
|
The easiest workaround is to simply use different variable names. Alternatively, you can [[ignore]] the check.
|
||||||
|
|
||||||
|
It is also possible to satisfy ShellCheck by declaring the `local` variable separately from assigning to it, e.g.:
|
||||||
|
|
||||||
**Problematic Code:**
|
|
||||||
```sh
|
```sh
|
||||||
foo () {
|
foo () {
|
||||||
local -a baz
|
local -a baz
|
||||||
@@ -43,21 +44,7 @@ foo () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bar () {
|
bar () {
|
||||||
local baz="qux"
|
local baz # ShellCheck gets confused if these lines are merged as local baz="qux"
|
||||||
echo "$baz"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Correct Code:**
|
|
||||||
```sh
|
|
||||||
foo () {
|
|
||||||
local -a baz
|
|
||||||
baz+=("foo" "bar")
|
|
||||||
echo "${baz[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
bar () {
|
|
||||||
local baz
|
|
||||||
baz="qux"
|
baz="qux"
|
||||||
echo "$baz"
|
echo "$baz"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user