Use of Linux read Command

  • 2021-08-21 22:10:27
  • OfStack

1. Brief introduction to commands

The read command is an Shell built-in command, which is used to read a single line from the file descriptor specified by the standard input or-u option, divide the read single line into multiple fields according to IFS variables, and assign the divided fields to the specified variable list var_name respectively. The first field is assigned to the first variable var_name1, and the second field is assigned to the second variable var_name2, in turn, to the end. If the specified variable name is less than the number of fields, the extra field is assigned to the last var_name along with the separator, and if the specified variable command is more than the number of fields, the extra variable is assigned to null. If no var_name is specified, all of the segmented fields are stored in the specific variable REPLY. Of course, it can not only assign variables, but also assign arrays.

The IFS (Internal Field Separator) variable is an environment variable built into Shell, which is used by the read command to separate a single read line into multiple fields. The default value is.

The REPLY variable is also a built-in environment variable of Shell, which is used to receive a single line read by the read command when the read command does not specify the receive variable.

2. Command format


read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

3. Description of options


-a [aname]: Split fields are stored in the specified array in turn, and the starting position of storage is from the subscript of the array 0 Begin 
-d [delim]: Heel 1 Identifier, only the first 1 Characters are useful to replace the newline character as the end flag of the line 
-e: You can use the command completion function when entering, and use Tab Key can automatically not complete the files in the current directory 
-i [text]:If readline is being used to read the line, text is placed into the editing buffer before editing begins
-n [nchars]: Heel 1 A number that defines the length of input text instead of reading the whole line 
-N [nchars]: Heel 1 A number that defines the length of input text instead of reading the whole line. But if 1 Insufficient operation nchars Characters, ignore the line separator and continue reading 1 Row 
-p [prompt] Print prompt information before input when reading input from terminal 
-r: Shielded backslash \ If this option is not available, the \ As 1 Escape characters, if any \ It's just a normal character 
-s: Quiet mode, which is no longer displayed on the screen when characters are entered, for example login Enter the password when 
-t [timeout]: Followed by seconds, defines the wait time for input characters 
-u [fd]: Followed by a file descriptor fd Read from the file descriptor 

4. Common examples

(1) If no variable is specified, read passes the incoming value to REPLY, which can be referenced by calling REPLY, REPLY and REPLY.


[root@TENCENT64 ~]# read;echo "\$REPLY:$REPLY"
dablelv
$REPLY:dablelv

(2) 1 prompt is specified when read is read from the terminal


[root@TENCENT64 ~]# read -p"input u password:";echo "\$REPLY:$REPLY"
input u password:123456
$REPLY:123456

(3)-The t parameter specifies the number of seconds the read command waits for input, and when the time is full, the read command returns a non-zero exit status code.


#!/bin/bash

if read -t 5 -p " Enter a Web site name :" name
then
  echo " The website name you entered is  $website"
else
  echo "\n Sorry, you timed out. "
fi
exit 0

Execute the program without input, wait for 5 seconds:

Enter a Web site name:
Sorry, you timed out

(4) In addition to controlling the input time, you can also use the-n option to control the number of characters entered. When the number of input characters reaches a predetermined number, it automatically exits and assigns the input data to the variable. For example, take only 2 inputs and exit:


#!/bin/bash

read -n2 -p " Please enter two characters at will : " any
echo "\n The two characters you entered are :$any"
exit 0

(5)-The s option enables the input data not to be displayed on the command terminal (in fact, the input content is displayed, except that the read command sets the text color to the same color as the background). This option is commonly used for entering passwords.


#!/bin/bash

read -s -p " Please enter your password :" pass
echo "\n The password you entered is  $pass"
exit 0

It is not displayed after the execution program enters the password:

Please enter your password:
The password you entered is runoob

(6) Reading files

Every time the read command is called, the "1 line" text in the file is read. When the file has no readable lines, the read command exits in a non-zero state.


while read var1 var2
do
	echo $var1 $var2
done < file.txt

The above is the use of Linux read command details, more information about Linux read command please pay attention to other related articles on this site!


Related articles: