How to use Linux read command?

The read command in Linux is used to read a line from the standard input. It then splits the line into words the first word is assigned to the first variable, the second word is assigned to the second variable, and so on.

In this article, you will learn how to use Linux read command with some examples.

The syntax of Linux read command

The basic syntax of how to use the Linux read is given below.

read [Option] [Var..]

You can find the options to use with the read command on its man page. Using the option -r will make the read command not treat the backslash character in any special way. It will consider each backslash to be part of the input line.

Usage of bash read command in Linux

The following examples show the usage of the bash read command in Linux.

Using read command without any arguments

When no arguments are provided the read command the whole line entered by a user will be assigned to REPLY variable.

echo $REPLY

Now save this in a file with .sh extension and execute it. In the output of this program, the same line will get printed which is entered by the user.

Taking user input into multiple variables

You can pass the variables as the argument to read command to store the input provided by the user. The whole sentence will get split into words and each word will be assigned to the corresponding variables.

read var1 var2 var3
echo $var1
echo $var2
echo $var3

When you execute this script it will produce the given output.

When the number of words in the sentence entered by the user is greater than the number of variables. The last variable will store the remaining words of the sentence after the assignment. OR if the number of variables is greater than the words in the sentence then the remaining variables will be assigned with the empty values.

Changing the delimiter

By default input taken using the read command is split into words using one or more spaces, tabs, and newline as delimiters. You can use another character as a delimiter by assigning it to IFS (internal field separator) variable.

You can see this in the example below.

echo "zero:one:two" | (IFS=":" read -r var1 var2 var3; echo -e "$var1 \n$var2 \n$var3")

This will produce the given output:

Here the string zero:one:two  is split based on : character.

Assign values to an array of variables

You can use the array to assign words of a sentence inputted by a user. For this, you need to invoke the read command with the option -a.

read -r -a ARR <<< "Linux is an open-source operating system."

for i in "${ARR[@]}"; do
echo "$i"

On executing this script it will produce the given output.

Reading input from a file

Reading input from a file can be very useful when we are writing long scripts. We can use the read command to take input from a file.

The below are some simple structured data of cars.

car,car model,car year,car vin

Now to extract specific values from this file we will use the given bash code.

      exec {file_descriptor}<"./filename.csv"
      declare -a arr
      while IFS="," read -a arr -u $file_descriptor
                    echo "${arr[0]},${arr[2]}"
      exec {file_descriptor}>&-

Here exec command is used to open the file descriptor on our input. Then we pass it to read command using while loop this allows us to read input line by line. Finally, we will again use the exec command to close the file descriptor.

The above code will produce the given output.


Now I hope you understand how to use the bash read command in Linux. If you have a query then write us in the comments below.

Leave a Comment