mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2002 (markdown)
10
SC2002.md
10
SC2002.md
@@ -3,15 +3,15 @@
|
|||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cat file | tr ' ' _ | grep a_
|
cat file | tr ' ' _ | nl
|
||||||
cat file | ( while read i; do echo "${i%?}"; done )
|
cat file | while IFS= read -r i; do echo "${i%?}"; done
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
< file tr ' ' _ | grep a_ # **simple commands only, won't work with compounds
|
< file tr ' ' _ | nl
|
||||||
( while read i; do echo "${i%?}"; done ) < file # postfix works for everything
|
while IFS= read -r i; do echo "${i%?}"; done < file
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
@@ -24,4 +24,4 @@ Many tools also accept optional filenames, e.g. `grep -q foo file` instead of `c
|
|||||||
|
|
||||||
### Exceptions
|
### Exceptions
|
||||||
|
|
||||||
None.
|
Pointing out UUOC is a long standing shell programming tradition, and removing them from a short-lived pipeline in a loop can speed it up by 2x. However, it's not necessarily a good use of time in practice, and rarely affects correctness. [[Ignore]] as you see fit.
|
Reference in New Issue
Block a user