Provide an example and rationale for this optional warning

Kevin Cathcart
2021-01-20 11:22:21 -05:00
parent c80dd10ad1
commit 83748c5b5d

@@ -1,3 +1,25 @@
# Warn about variable references without braces.
This error is optional and not enabled by default.
This error is optional and not enabled by default.
## Problematic code:
```shell
subdir='example'
cd ${subdir}
```
## Correct code:
```shell
subdir='example'
cd "${subdir}"
```
## Rationale:
Shellcheck normally warns about unquoted variable use due to potential globbing or word splitting issues. See [[SC2086]] for details. However if it is determined that a variable does not have have spaces or special characters it will omit that warning. This optional warning exists to suggest that quotes be used even in this scenario. If the code is later changed such that special characters can appear in the variable, having its use already quoted will prevent issues.
This optional warning is also helpful if shellcheck's analysis of the variable contents is wrong because of indirect modification of the variable or because unknown commands implemented as shell functions have modified the variable.