From 83748c5b5d9331d5ebf473d7c7a6793b55b6eb25 Mon Sep 17 00:00:00 2001 From: Kevin Cathcart Date: Wed, 20 Jan 2021 11:22:21 -0500 Subject: [PATCH] Provide an example and rationale for this optional warning --- SC2248.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/SC2248.md b/SC2248.md index 31de625..f22d8fc 100644 --- a/SC2248.md +++ b/SC2248.md @@ -1,3 +1,25 @@ # Warn about variable references without braces. -This error is optional and not enabled by default. \ No newline at end of file +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. \ No newline at end of file