Guide to the Linux read Command


Introduction

As we work with the Linux command line, we often need to interact with input from the user or from files. This is where the "read" command comes in handy. It is a simple yet powerful command that can be used in a variety of ways to process input in Linux. By learning how to use the read command in various ways, we can become more proficient in using this command and automate tasks more efficiently.

Let us cover all the methods and formats of the Linux read command. We will start with the basics, including how to read input from the user and how to customize the input delimiter. We will then move on to more advanced topics, such as reading input from files and from other commands.

Basic Methods of Using the read Command

The most basic use of the read command is to read input from the user. To do this, simply type "read" followed by a variable name. For example −

$ read name
Somdeb
$ echo "Hello $name"

When this code is run, the user will be prompted to enter their name. Once they have entered their name, the value will be stored in the variable "name" and the script will output "Hello" followed by the name.

Hello Somdeb

Advanced Methods of Using the read Command

The read command in Linux has advanced options that allow for more flexibility and control over how input is processed. Let us explore these advanced methods −

  • -d delim − This option allows us to specify the delimiter of the input line, rather than using the default newline character.

  • -u fd − By default, the read command reads input from standard input (stdin), but with this option, we can read input from a specific file descriptor.

  • -r − Normally, the read command treats the backslash character () as an escape character, but with this option, the backslash is treated as a regular character.

  • -t timeout − This option sets a timeout for reading input, attempting to read the input for a given period of seconds. If the timeout is reached before input is received, the read command terminates.

  • –p prompt − displays prompt text before requesting input, enhancing user experience.

Read command has several advanced methods that can be used to customize its behavior. One of these method is the -t" option, which allows us to set a timeout for the read command. This is useful when we want to read input from the user, but do not want to wait indefinitely for them to enter input.

$ if read -t 10 -p "Enter your name: " name; then
   echo "Hello $name"
else
   echo "Timed out!!"
fi

In this code, the "-t" option is used to set a timeout of 10 sec for the read command. If the user enters their name within 10 seconds, it will be stored in the variable "name" and the script will output "Hello" followed by the user's name. If the user does not enter their name within 10 seconds, the script will output "Timed out!!".

Enter your name: Somdeb Nath
Hello Somdeb Nath

Another advanced method is the ability to read multiple values into multiple variables. To do this, simply list the variable names after the "read" command, separated by spaces.

$ read firstname middlename lastname
Sk. Adil Hussain
$ echo "Hello [$firstname] [$middlename] [$lastname]"

In this code, the user is prompted to enter their first name, middle name and last name. Once they have entered the values, they are stored in the variables "firstname", “middlename’ and "lastname", respectively. And the script will return the following output −

Hello [Sk.] [Adil] [Hussain]

Using the read Command for Reading Input From Files

Reading input from files can be very useful when we want to fetch specific fields of data. For example, let's consider a file containing data for bikes (file.txt) −

bike,model,year,vin
Honda,CRF450R,2022,JH2PE1030NK200013
Kawasaki,Z400,2021,JKAZR2A10MA042809
Yamaha,YZF-R6,2020,JYARJ28E8LA000081

To read this, we can use loop to read each line of the file and process it.

$ while read line; do
   echo $line
done < file.txt

In this code, the "while" loop reads each line of the file "file.txt" and stores it in the variable "line". The "echo" command then outputs the value of "line". This loop will continue until all lines in the file have been read.

Using the read Command for Reading Input From Files

When using the read command, we can also read input from other commands, which can be quite useful for processing large amounts of data. One common use case is to redirect the output of a command to the input of the read command.

Here's an example of how to redirect the output of the ls command and extract the file names and their access rights from the root folder −

$ ls -l / | while read -r line; do
   filename=$(echo $line | awk '{print $9}')
   rights=$(echo $line | awk '{print $1}')
   echo "File: $filename, Access Rights: $rights"
done

We use the ls -l / command to list all files in the root folder with their access rights. The output is piped to a while loop that reads each line using the read command. The awk command extracts the filename and access rights from each line, which are printed using the echo command.

File: , Access Rights: total
File: bin, Access Rights: lrwxrwxrwx
File: boot, Access Rights: drwxr-xr-x
File: cdrom, Access Rights: drwxrwxr-x
File: dev, Access Rights: drwxr-xr-x
File: etc, Access Rights: drwxr-xr-x

Conclusion

The read command is a powerful tool for interacting with the Linux terminal. It can be used to read input from the user or from a file, and it has several advanced options that allow us to customize its behavior. By mastering the read command, we can become a more effective and efficient Linux user.

Updated on: 29-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements