Reading a value into a variable

The read builtin command with its -r option can be used to read values into variables from standard input, including directly from the terminal:

$ read -r myname
Bash User

Press Ctrl + D (end-of-file) after pressing Enter after the last preceding line; the entire first line of input will be used as the value for myname:

bash$ declare -p myname
declare -- myname='Bash User'

You should always use the -r option with read, to avoid running into confusing issues with reading backslashed values.

We will use the read builtin a lot more in Chapter 6, Loops and Conditionals,as part of Bash's looping syntax for processing data.