BASH - Files - Read a file - Read from an interactive shell

read foo

NOTE: This will store the input into a variable named $foo.


Handling backslash interpretation

NOTE: The -r option is often used with read:

  • -r: Prevents backslash interpretation (usually used as a backslash newline pair, to continue over multiple lines or to escape the delimiters).
    • Without this option, any unescaped backslashes in the input will be discarded.
    • You should almost always use the -r option with read.

The most common exception to this rule is when -e is used, which uses Readline to obtain the line from an interactive shell.

  • In that case, tab completion will add backslashes to escape spaces and such, and you do not want them to be literally included in the variable.
  • This would never be used when reading anything line-by-line, though, and -r should always be used when doing so.