mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Make explanation on how to fix this check on sh clearer, add example of heredoc
10
SC2031.md
10
SC2031.md
@@ -18,7 +18,15 @@ n=0
|
||||
while read i; do (( n+=i )); done < <(printf "%s\n" {1..10})
|
||||
echo $n
|
||||
```
|
||||
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.
|
||||
In `sh`, temporary files, FIFOs or file descriptors can be used instead. When the output of the command can be stored to a variable before entering the loop, here documents are a preferable alternative:
|
||||
```sh
|
||||
n=0
|
||||
SUMMANDS="$(printf '%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n' 1 2 3 4 5 6 7 8 9 10)"
|
||||
while read i; do n=$(( n + i )); done <<SUMMANDS_INPUT
|
||||
$SUMMANDS
|
||||
SUMMANDS_INPUT
|
||||
echo $n
|
||||
```
|
||||
|
||||
|
||||
With Bash 4.2+ you can also use `shopt -s lastpipe` which will change the pipe behaviour to be similar to Ksh and Zsh (see Rationale below) [as long as job control is not active](https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html) (e.g. inside a script):
|
||||
|
Reference in New Issue
Block a user