mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2031 (markdown)
@@ -4,18 +4,22 @@
|
|||||||
|
|
||||||
There are many ways of accidentally creating subshells, but a common one is piping to a loop:
|
There are many ways of accidentally creating subshells, but a common one is piping to a loop:
|
||||||
|
|
||||||
|
```bash
|
||||||
n=0
|
n=0
|
||||||
printf "%s\n" {1..10} | while read i; do (( n+=i )); done
|
printf "%s\n" {1..10} | while read i; do (( n+=i )); done
|
||||||
echo $n
|
echo $n
|
||||||
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
# Bash specific:
|
```bash
|
||||||
|
# Bash specific: process substitution. Also try shopts like lastpipe.
|
||||||
n=0
|
n=0
|
||||||
while read i; do (( n+=i )); done < <(printf "%s\n" {1..10})
|
while read i; do (( n+=i )); done < <(printf "%s\n" {1..10})
|
||||||
echo $n
|
echo $n
|
||||||
|
```
|
||||||
|
|
||||||
In `sh`, a temp file can be used instead of process substitution.
|
In `sh`, a temp file (better if fifo or fd) can be used instead of process substitution. And if it's acceptable to do it with waiting, try Here Documents.
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user