describe workaround to local variable confusion

Maddison Hellstrom
2020-02-09 17:00:15 -08:00
parent 4d7b4d7958
commit 058d9fd73c

@@ -39,3 +39,34 @@ Another possible cause is accidentally missing the `$` on a previous assignment:
### Exceptions: ### Exceptions:
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. 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.
In the case of local variables, a workaround is to declare the local variable separately from assigning to it:
**Problematic Code:**
```sh
foo () {
local -a baz
baz+=("foo" "bar")
echo "${baz[@]}"
}
bar () {
local baz="qux"
echo "$baz"
}
```
**Correct Code:**
```sh
foo () {
local -a baz
baz+=("foo" "bar")
echo "${baz[@]}"
}
bar () {
local baz
baz="qux"
echo "$baz"
}
```