mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2328 (markdown)
36
SC2328.md
Normal file
36
SC2328.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
## This redirection takes output away from the command substitution.
|
||||||
|
|
||||||
|
(and companion warning "This command substitution will be empty because the command's output gets redirected away" which points to the relevant command substitution)
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
var=$(tr -d ':' < input.txt > output.txt)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# If the output should be captured INSTEAD OF being written to file
|
||||||
|
var=$(tr -d ':' < input.txt)
|
||||||
|
|
||||||
|
# If the output should be captured IN ADDITION to being written to file
|
||||||
|
var=$(tr -d ':' < input.txt | tee output.txt)
|
||||||
|
|
||||||
|
# If the output should NOT BE captured at all
|
||||||
|
tr -d ':' < input.txt > output.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck has found a command substitution (`$(..)`, `` `..` ``) that appears to never capture any output because the command's output is being redirected.
|
||||||
|
|
||||||
|
Decide whether you want the output to be captured (by removing the redirection), to go into wherever it's redirected (by not running the command in a command substitution), or both (by using `tee` to copy the output to both file and stdout).
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user