Transferred from: http://www.1987.name/151.html
The Read command is an internal command for reading input from a terminal or a file, and the Read command reads an entire line of input, and the newline character at the end of each line is not read into. After the read command, if no variable name is specified, the read data is automatically assigned to the specific variable reply. The following list shows the common ways of the Read command:
Read 1987name reads input from the standard input and assigns a value to the variable 1987name.
Read first last reads the input from the standard input to the first space or carriage return, puts the input word into the variable, and places the other input into the last variable.
Read reads a row from the standard input and assigns a value to the specific variable reply.
Read-A arrayname reads the list of words into the arrayname array.
" text " Print the hint (text), wait for input, and store the input in reply.
The read-R line allows the input to contain a backslash.
3 Specifies that the read wait time is 3 seconds.
2 var reads two characters from the input and stores the variable Var, no need to press ENTER to read.
" : " var end the input line with the delimiter ":".
Read Command example
Reads the input from the standard input and assigns a value to the variable 1987name.
#read 1987name #等待读取输入 until the Enter is complete, and the input is assigned to the variable answerhelloworld #控制台输入Hello #echo $1987name # Print variable HelloWorld
Wait for a set of inputs, separated by a space between each word, until the carriage return ends, and the words are assigned to the three read-in variables, respectively.
#read One and three 1 2 3 2 3 "one = $one, one = $two, three = $three"12 3
Reply Example
#read is REPLY #在控制台输入该行. #echo $REPLY is REPLY
-P Option Example
""#输出文本提示, while waiting for input and assigning the result to reply. Enter you name:stephen #在提示文本之后输入stephen #echo $REPLYstephen
Waits for console input and treats the input information as an array, assigns the array variable friends, and the input information separates each element of the array with a space.
#read-"they is ${friends[0]}, ${friends[1]} and ${friends[2]}. " They is Tim, Tom and Helen.
Add a terminal input password, do not let the password display example.
Method 1:
#!/bin/" Enter password:" -is"$pwd"
Method 2:
#!/bin/-" Enter password:" pwdstty echoechoecho input completed.
Where option-echo disables sending the output to the terminal, and the option echo allows the output to be sent.
[Shell Learning notes] Read command get standard input from keyboard or file (reprint)