mirror of
https://github.com/tldr-pages/tldr
synced 2026-08-26 14:13:36 +01:00
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:
@@ -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`
|
||||
|
||||
Reference in New Issue
Block a user