read

Read variable value from keyboard

Supplementary instructions

read command reads the value of a variable from the keyboard, usually used in shell scripts to interact with users. This command can read the values of multiple variables at one time. The variables and input values need to be separated by spaces . After the read command, if no variable name is specified, the read data will be automatically assigned to the specific variable REPLY

grammar

read(option)(parameter)

Options

-p: Specifies the prompt when reading the value;
-t: Specifies the time (in seconds) to wait when reading values.

Parameters

Variable: Specify the variable name to read the value.

Example

The following list shows the common ways of using the read command:

read 1987name
Read input from standard input and assign it to variable 1987name.
read first last
Read the input from the standard input to the first space or carriage return, put the first word entered into the variable first, and put the other input on the line into the variable last.
read
Reads a line from standard input and assigns it to the specified variable REPLY.
read -a arrayname
Read the word list into the arrayname array.
read -p "text"
Prints the prompt (text), waits for input, and stores the input in REPLY.
read -r line
Allow input to contain backslashes.
read -t 3
Specify the read wait time to be 3 seconds.
read -n 2 var
Read two characters from the input and store them in the variable var without pressing Enter to read.
read -d ":" var
End the input line with the delimiter ":".

read command example

Read input from standard input and assign it to variable 1987name.

#read 1987name #Wait to read the input until the input is completed after pressing Enter, and assign the input to the variable answer
HelloWorld #Console input Hello

#echo $1987name #Print variables
HelloWorld

Wait for a set of input, with each word separated by a space until the carriage return ends, and assign the words to the three read variables in sequence.

#read one two three
1 2 3 #Enter 1 2 3 in the console, separated by spaces.

#echo "one = $one, two = $two, three = $three"
one = 1, two = 2, three = 3

REPLY example

#read #Wait for console input and assign the result to the specific built-in variable REPLY.
This is REPLY #Enter this line in the console.

#echo $REPLY #Print out the specific built-in variable REPLY to confirm whether it is assigned correctly.

This is REPLY

-p option example

#read -p "Enter your name: " #Output text prompts, wait for input, and assign the result to REPLY.
Enter you name: stephen #Enter stephen after the prompt text

#echo $REPLY
stephen

Wait for console input, treat the input information as an array, and assign it to the array variable friends. The input information uses spaces to separate each element of the array.

#read -a friends
Tim Tom Helen

#echo "They are ${friends[0]}, ${friends[1]} and ${friends[2]}."
They are Tim, Tom and Helen.

**Added an example of not allowing the password to be displayed when entering the password on the terminal. **

method 1:

#!/bin/bash
read -p "Enter password:" -s pwd
echo
echo password read, is "$pwd"

Method 2:

#!/bin/bash
stty-echo
read -p "Enter password:" pwd
stty echo
echo
echo input completed.

Among them, option -echo disables sending output to the terminal, while option echo allows output to be sent.

Use the read command to read the variable value from the keyboard and assign the value to the specified variable. Enter the following command:

read v1 v3 #Read variable value

After executing the above command, you are required to enter two data, as shown below:

Linux c+ #Input data

After completion, you can use the echo command to output and view the specified variable value. Enter the following command:

echo $v1 $v3 #Output the value of the variable

After executing the command to output variable values, the data value entered by the user will be displayed, as shown below:

Linux c+ #Output variable value

Note: When using the echo command to output variable values, you must add the symbol $ before the variable name. Otherwise, echo will directly output the variable name.