read: add IFS= and -r to while loop example (#23332)

Without `IFS=`, the `line` variable will have leading
and trailing whitespace stripped. Without `-r`, each
line will have backslashes interpreted as escape
characters. This is surprising behavior when trying
to perform an action on each line, and nearly all
examples of this pattern I've seen in blogs etc.
include IFS= and -r for this reason.

Explanation of IFS= and -r:
https://unix.stackexchange.com/questions/209123/understanding-ifs-read-r-line

Shell check lint for this:
https://www.shellcheck.net/wiki/SC2013

Co-authored-by: jxu <7989982+jxu@users.noreply.github.com>
This commit is contained in:
aftix
2026-08-01 18:51:58 -04:00
committed by GitHub
co-authored by jxu
parent e0062cfb8b
commit 367061c5f3
+2 -2
View File
@@ -31,6 +31,6 @@
`read -s {{variable}}`
- Read `stdin` and perform an action on every line:
- Perform an action on each line of a command's output:
`cat {{/dev/stdin|path/to/file|...}} | while read line; do {{echo|ls|rm|...}} "$line"; done`
`{{command}} | while IFS= read -r line; do {{echo|ls|rm|...}} "$line"; done`