mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2209 (markdown)
27
SC2209.md
Normal file
27
SC2209.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
## Use var=$(command) to assign output (or quote to assign string).
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
user=whoami # Want to run whoami and assign output
|
||||||
|
|
||||||
|
PAGER=cat git log # Want to assign the string "cat"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
user=$(whoami)
|
||||||
|
|
||||||
|
PAGER="cat" git log
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Putting `var=` in front of a command will not assign its output. Use `var=$(my command here)` to execute the command and capture its output.
|
||||||
|
|
||||||
|
If you do want to assign a literal string, use quotes to make this clear to shellcheck and humans alike.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
Reference in New Issue
Block a user