mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Add another required case for using $ in an arithmetic expression, one that too many people tend to forget about.
12
SC2004.md
12
SC2004.md
@@ -25,3 +25,15 @@ $ echo $((a * 5)) # evaluates as (1+1)*5
|
|||||||
```
|
```
|
||||||
|
|
||||||
The `$` is unavoidable for special variables like `$1` vs `1`, `$#` vs `#`. It's also required when adding modifiers to parameters expansions, like `${#var}` or `${var%-}`. ShellCheck does not warn about these cases.
|
The `$` is unavoidable for special variables like `$1` vs `1`, `$#` vs `#`. It's also required when adding modifiers to parameters expansions, like `${#var}` or `${var%-}`. ShellCheck does not warn about these cases.
|
||||||
|
|
||||||
|
The `$` is also required (and not warned about) when you need to specify the *base* for a variable value:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ a=09
|
||||||
|
$ echo $((a + 1)) # leading zero forces octal interpretation
|
||||||
|
bash: 09: value too great for base (error token is "09")
|
||||||
|
$ echo $((10#a + 1))
|
||||||
|
bash: 10#a: value too great for base (error token is "10#a")
|
||||||
|
$ echo $((10#$a + 1))
|
||||||
|
10
|
||||||
|
```
|
Reference in New Issue
Block a user