Files
tldr/pages/common/read.md
T
aftixandjxu 367061c5f3 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>
2026-08-01 18:51:58 -04:00

892 B

read

Shell builtin for retrieving data from stdin. More information: https://www.gnu.org/software/bash/manual/bash.html#index-read.

  • Store data that you type from the keyboard:

read {{variable}}

  • Store each of the next lines you enter as values of an array:

read -a {{array}}

  • Specify the number of maximum characters to be read:

read -n {{character_count}} {{variable}}

  • Assign multiple values to multiple variables:

read <<< "{{The surname is Bond}}" {{_ variable1 _ variable2}}

  • Do not let backslash (\) act as an escape character:

read -r {{variable}}

  • Display a prompt before the input:

read -p "{{Enter your input here: }}" {{variable}}

  • Do not echo typed characters (silent mode):

read -s {{variable}}

  • Perform an action on each line of a command's output:

{{command}} | while IFS= read -r line; do {{echo|ls|rm|...}} "$line"; done