diff --git a/SC2002.md b/SC2002.md index 420ac82..5c8f3be 100644 --- a/SC2002.md +++ b/SC2002.md @@ -3,15 +3,15 @@ ### Problematic code: ```sh -cat file | tr ' ' _ | grep a_ -cat file | ( while read i; do echo "${i%?}"; done ) +cat file | tr ' ' _ | nl +cat file | while IFS= read -r i; do echo "${i%?}"; done ``` ### Correct code: ```sh -< file tr ' ' _ | grep a_ # **simple commands only, won't work with compounds -( while read i; do echo "${i%?}"; done ) < file # postfix works for everything +< file tr ' ' _ | nl +while IFS= read -r i; do echo "${i%?}"; done < file ``` ### Rationale: @@ -24,4 +24,4 @@ Many tools also accept optional filenames, e.g. `grep -q foo file` instead of `c ### 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. \ No newline at end of file