SC2206: Exception for integer variables
For bugs
- Rule Id (if any, e.g. SC1000): SC2206
- My shellcheck version (
shellcheck --versionor "online"): online (26th June 2024) and tried older version 0.7.0. - The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
- I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit
For new checks and feature suggestions
- https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this: N/A
- I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related
Here's a snippet or screenshot that shows the problem:
#!/usr/bin/env bash
declare -gir -- i=0
declare -gai -- arr
arr+=( ${i} )Here's what shellcheck currently says:
Line 5:
arr+=( ${i} )
^-- SC2206 (warning): Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.Here's what I wanted or expected to see:
No warning. As far as I am aware there is no need to quote as we know ${i} is (and in this example, always will be) an integer, which will never be split or globbed.
N.b. in my above statement I said "there is no need to quote" instead of "there is no reason to quote", as if you are a heaven who deals with variables without values (without explicitly stating defaults), then there is a reason to use the quoted version for integer variables. Take this example:
#!/usr/bin/env bash
declare -gir -- i # No value set!
declare -gai -- arr
arr+=( ${i} )which produces: declare -ai arr=().
Now, if we were to use quotes: arr+=( "${i}" ) then it would produce: declare -ai arr=([0]="0"), since "${i}" evaluates to "" (an empty string) which when converted to an integer is 0.
The same is similar for if it was a regular array (declare -ga -- arr), the quoted version (arr+=( "${i}" )) would add a value (declare -a arr=([0]="")), but the unquoted version (arr+=( ${i} )) would not (declare -a arr=()).
Since I believe the unquoted version to be valid even when dealing with variables without values, I believe my proposed exception still stands.
Source: koalaman/shellcheck