Linux Comm Read
[ Linux Command Manual](#)
The Linux `read` command is used to read values from standard input.
The `read` builtin command is used to read a single line of data from standard input. This command can be used to read keyboard input, and when used with redirection, it can read a line of data from a file.
### Syntax
read [name ...]
**Parameter Description:**
* `-a` followed by a variable, which will be treated as an array and assigned values, with space as the default delimiter.
* `-d` followed by a delimiter character; only the first character after it is useful as the ending delimiter.
* `-p` followed by a prompt message, which is printed before input.
* `-e` enables command completion functionality during input.
* `-n` followed by a number, defining the length of the input text, which is very practical.
* `-r` disables backslash escaping. Without this option, `` acts as an escape character; with it, `` is treated as a normal character.
* `-s` silent mode, where input characters are not displayed on the screen, for example, when entering a password during login.
* `-t` followed by seconds, defining the wait time for input characters.
* `-u` followed by a file descriptor, to read from that file descriptor, which can be newly opened with `exec`.
### Examples
**1. Simple Read**
#!/bin/bash# This will default to a new line echo "Enter website name: " # Read input from the keyboard read website echo "You entered: $website" exit 0 # Exit
Test result:
Enter website name: www. You entered: www.
**2. The `-p` parameter allows specifying a prompt directly in the `read` command line.**
#!/bin/bash read -p "Enter website name:" website echo "You entered: $website" exit 0
Test result:
Enter website name:www. You entered: www.
**3. The `-t` parameter specifies the number of seconds the `read` command waits for input. When the timer expires, `read` returns a non-zero exit status.**
#!/bin/bashif read -t 5 -p "Enter website name:" website then echo "You entered: $website"else echo "nSorry, you timed out."fiexit 0
Run the program without input, wait 5 seconds:
Enter website name:Sorry, you timed out
4. Besides timing the input, you can also use the **`-n`** parameter to set the **`read`** command to count input characters. When the number of input characters reaches the predetermined number, it exits automatically and assigns the input data to the variable.
#!/bin/bash read -n1 -p "Do you want to continue [Y/N]?" answer case $answer in Y | y) echo "fine ,continue";; N | n) echo "ok,good bye";;*) echo "error choice";;esacexit 0
This example uses the `-n` option followed by the number 1, instructing the `read` command to exit as soon as it receives one character. As soon as a character is pressed as a reply, the `read` command immediately accepts the input and passes it to the variable, without needing to press Enter.
Exit after receiving only 2 input characters:
#!/bin/bash read -n2 -p "Please enter any two characters: " any echo "nThe two characters you entered are:$any"exit 0
Run the program and enter two characters:
Please enter any two characters: 12The two characters you entered are:12
5. The **`-s`** option makes the data entered in the **`read`** command not displayed on the command terminal (in fact, the data is displayed, but the **`read`** command sets the text color to be the same as the background color). This option is commonly used when entering passwords.
#!/bin/bash read -s -p "Please enter your password:" pass echo "nYour password is $pass"exit 0
Run the program and enter a password; it is not displayed:
Please enter your password:Your password is tutorial
**6. Reading a File**
Each call to the `read` command reads "one line" of text from the file. When there are no readable lines left in the file, the `read` command exits with a non-zero status.
How can data from a file be passed to `read`? Use the `cat` command and pipe its output directly to a `while` command containing the `read` command.
The test file `test.txt` has the following content:
123456 tutorial
Test code:
#!/bin/bash count=1 # Assignment statement, no spaces cat test.txt | while read line # The output of the cat command is used as input for the read command; the value read by read is placed in linedo echo "Line $count:$line" count=$[ $count + 1 ] # Note the spaces inside the brackets. done echo "finish"exit 0
Execution result:
Line 1:123Line 2:456Line 3:tutorial finish
Using the **`-e`** parameter, the following example will output related filenames (existing in the directory) after entering the character **`a`** and pressing the **Tab** key:
$ read -e -p "Enter filename:" str Enter filename:a a.out a.py a.pyc abc.txt Enter filename:a
[ Linux Command Manual](#)
YouTip