Updated SC2031 (markdown)

Mingye Wang
2015-09-30 09:37:05 -04:00
parent b4542ffe46
commit 4d7333cb68

@@ -4,18 +4,22 @@
There are many ways of accidentally creating subshells, but a common one is piping to a loop:
```bash
n=0
printf "%s\n" {1..10} | while read i; do (( n+=i )); done
echo $n
```
### Correct code:
# Bash specific:
```bash
# Bash specific: process substitution. Also try shopts like lastpipe.
n=0
while read i; do (( n+=i )); done < <(printf "%s\n" {1..10})
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: