mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2054 (markdown)
26
SC2054.md
Normal file
26
SC2054.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
## Use spaces, not commas, to separate array elements.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
flags=("-l", "-d", "--sort=size")
|
||||||
|
ls "${flags[@]}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
flags=("-l" "-d" "--sort=size")
|
||||||
|
ls "${flags[@]}"
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You appear to have used commas to separate array elements in an array assignment. Other languages require this, but bash instead treats the commas as literal strings.
|
||||||
|
|
||||||
|
In the problematic code, the first element is `-l,` with the trailing comma, and the executed command ends up being `ls -l, -d, --sort=size`.
|
||||||
|
|
||||||
|
In the correct code, there are no trailing commas and the command will be `ls -l -d --sort=size` as expected.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None (if you actually want a trailing comma in your strings, move it inside the quotes).
|
Reference in New Issue
Block a user