Read the value of a variable from the keyboard. The read command reads the value of a variable from the keyboard. It is commonly used in shell scripts to interact with the user. This command can read the values of multiple variables at a time. The values of variables and entered values must be separated by Spaces. After the read command, if no variable name is specified, the read data is automatically assigned to the specific variable REPLY
Command syntax
Read [option][parameter]
Command options
- -A is followed by a variable, which is treated as an array and assigned a value, with a space separator by default.
- -d is followed by an identifier, but only the first character after it is useful as an end identifier.
- -p Displays a prompt message before entering a value.
- -e You can use the command completion function when entering the command.
- -n followed by a number defines the length of the input text, which is useful.
- -r blocks \, if this option is not available, then \ is an escape character, otherwise \ is a normal character.
- -s Indicates the quiet mode. When a character is entered, for example, login, the password is not displayed on the screen.
- -t, followed by the number of seconds, defines the waiting time for the input character.
- -u, followed by fd, is read from the file descriptor, which can be newly opened by exec.
The command parameter
Variable: The name of the variable that specifies the read value.
Reads input from standard input and assigns values to variables
> read rumenz
123
> echo $rumenz
123
Copy the code
Read input from standard input to the first space or carriage return, place the first word entered into the variable rumenz1, and place the rest of the line of input into the variable rumenz2.
> read rumenz1 rumenz2
Copy the code
Reads a line from standard input and assigns it to the specific variable REPLY.
> read
123
> echo $REPLY
123
Copy the code
Read the list of words into the array of ArrayName.
> read -a arrayname
Copy the code
Print the prompt (text), wait for input, and store the input in REPLY.
> read -p "text:"
text:123
> echo $REPLY
123
Copy the code
Allows input to contain backslashes.
> read -r rumenz
Copy the code
Specify a read wait time of 3 seconds.
> read -t 2 rumenz
Copy the code
Reads two characters from the input and stores them in the variable var without pressing enter to read.
> read -n 2 rumenz
Copy the code
End the input line with the delimiter:.
> read -d ":" rumenz
Copy the code
Read in the array
> read -a rumenz
123 456 789
> echo ${rumenz[*]}
123 456 789
Copy the code
The terminal does not display the password when entering the password
> read -p "Enter the password :" -s pd password: > echo $pd 123456Copy the code
Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station