mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC1102 (markdown)
14
SC1102.md
14
SC1102.md
@@ -1,21 +1,21 @@
|
|||||||
## Shells differ in parsing ambiguous $(((. Use spaces: $( (( .
|
## Shells disambiguate $(( differently or not at all. If the first $( should start command substitution, add a space after it.
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
echo "$((( n > 0)) && mycommand --flags)"
|
echo "$((cmd "$@") 2>&1)"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
echo "$( (( n > 0)) && mycommand --flags)"
|
echo "$( (cmd "$@") 2>&1)"
|
||||||
```
|
```
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
You are using `$(((` (or `(((`) to mean `$( ((`: command expansion with an arithmetic command. The more common interpretation is `$(( (`: arithmetic expansion with parentheses.
|
You appear to be using `$((` with two (or more) parentheses in a row, where the first `$(` should open a subshell.
|
||||||
|
|
||||||
This is an ill-defined structure that is parsed differently between different shells and shell versions. Prefer adding a space to make it unambiguous, both to shells and humans.
|
This is an ill-defined structure that is parsed differently between different shells and shell versions. Prefer adding spaces to make it unambiguous, both to shells and humans.
|
||||||
|
|
||||||
Consider the `$(((` in `$(((1)) )`:
|
Consider the `$(((` in `$(((1)) )`:
|
||||||
|
|
||||||
@@ -23,4 +23,6 @@ Ash, dash and Bash 1 parses it as `$(( (` and subsequently fail to find the matc
|
|||||||
|
|
||||||
### Exceptions:
|
### Exceptions:
|
||||||
|
|
||||||
None.
|
**Alternatively**, you may indeed have correctly spaced your parentheses, but ShellCheck failed to parse `$((` as an arithmetic expression while accidentally succeeding in parsing it as `$(` + `(`.
|
||||||
|
|
||||||
|
In these cases, double check the syntax to ensure ShellCheck can parse the `$((`, or ignore this error and hope that it won't affect analysis too severely.
|
Reference in New Issue
Block a user