Easy Tutorial
❮ Linux Comm Uucp Linux Comm Ex ❯

Linux read Command

Linux Command Manual

The Linux read command is used to read values from standard input.

The read internal command is used to read a single line of data from standard input. This command can be used to read keyboard input or a line of data from a file when using redirection.

Syntax

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

Parameter Description:

Examples

1. Simple Reading

#!/bin/bash

# This will default to a new line  
echo "Enter website name: "  
# Read input from the keyboard  
read website  
echo "The website name you entered is $website"  
exit 0  # Exit

Test result:

Enter website name: 
www.tutorialpro.org
The website name you entered is www.tutorialpro.org

2. -p Parameter, allows specifying a prompt directly in the read command line.

#!/bin/bash

read -p "Enter website name:" website
echo "The website name you entered is $website" 
exit 0

Test result:

Enter website name:www.tutorialpro.org
The website name you entered is www.tutorialpro.org

3. -t Parameter specifies the number of seconds for the read command to wait for input. When the timer expires, the read command returns a non-zero exit status.

#!/bin/bash

if read -t 5 -p "Enter website name:" website
then
    echo "The website name you entered is $website"
else
    echo "\nSorry, you have timed out."
fi
exit 0

Executing the program without input, waiting for 5 seconds:

Enter website name:
Sorry, you have timed out

4. In addition to timing the input, the -n parameter can be used to count the number of characters input by the read command. When the number of input characters reaches the specified number, the command automatically exits 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";;

esac
exit 0

This example uses the -n option with a value of 1, indicating that the read command should exit after receiving one character. Simply press one character to answer, and the read command immediately accepts the input and assigns it to the variable without pressing the Enter key.

Accepts only 2 characters and then exits:

#!/bin/bash

read -n2 -p "Please enter any two characters: " any
echo "\nThe two characters you entered are:$any"
exit 0

Executing the program and entering two characters:

Please enter any two characters: 12
The two characters you entered are:12

5. The -s option can make the input data of the read command not displayed on the command terminal (actually, the data is displayed, but the read command sets the text color to the same as the background color). This option is commonly used for entering passwords.

#!/bin/bash

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

Executing the program, the password input is not displayed:

Please enter your password:
The password you entered is tutorialpro

6. Reading from a File

Each call to the read command reads a "line" of text from the file. When there are no more lines to read from the file, the read command exits with a non-zero status.

How can the data from the file be passed to the read command? Use the cat command and pipe the result directly to the while command containing the read command.

The content of the test file test.txt is as follows:

123
456
tutorialpro
#!/bin/bash

count=1    # Assignment statement, no spaces
cat test.txt | while read line      # Output of cat command is input to read command, read stores the value in line
do
   echo "Line $count:$line"
   count=$[ $count + 1 ]          # Note the spaces in the brackets.
done
echo "finish"
exit 0

Execution result:

Line 1:123
Line 2:456
Line 3:tutorialpro
finish

Using the -e parameter, the following example outputs related filenames (existing in the directory) after typing 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

❮ Linux Comm Uucp Linux Comm Ex ❯