×

Search anything:

read command in Linux

Free book on Dynamic Programming

Get this book -> Problems on Array: For Interviews and Competitive Programming

We use the read command to read from standard input or file descriptors. In this article, we learn about the read command in Linux.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Basics.
  4. Variables.
  5. Prompts.
  6. Limiting input.
  7. Hiding input.
  8. Read with here strings
  9. Arrays.
  10. Read command timeout.
  11. Internal field separator(IFS).
  12. Return values.
  13. Summary.
  14. References.

Introduction.

The read command is a built-in command in Linux that reads from a line from the standard input or a file descriptor and splits the line into words.

Syntax.

The syntax is as follows:

$ read [options] [name...]

In the options field, we specify options such as -p which prompts a user for input, -n for limiting output, -s for hiding sensitive inputs such as passwords, and much more.

In the name parameter we specify variables that will store the actual words that result from the split operation.

Basics.

To begin, let's execute the read command without any arguments and analyze its behavior.

$ read

read1

After executing the command, we can type in any text. This will be assigned to the $REPLY built-in variable. We can confirm this by echoing the value of $REPLY

$ echo $REPLY

All the read input is assigned to the $REPLY variable since we don't provide the read command with any arguments

Variables.

Now to specify variables with the read command:

$ read var1 var2 var3

After we execute the above command, we can type in text that will later be segmented into an individual string.

In this case, we have declared three variables to hold individual strings, however, if we have more than three words, the third word is crammed together into the third variable, var3 together with the other remaining words.

Let's see this in action:

$ read var1 var2 var3
Testing Variables \
> with the \
> read command

read10

To print output using printf command, we write:

$ aprintf "Variable1: $var1\nVariable2: $var2\nVariable3: $var3"

From the printf output, we have the last four words crammed into the third variable, also, we have passed multiple lines of input using the \ backslash character. This improves readability.

Prompts.

We can also use the -p option to prompt the user for input:

$ read -p "Hello, Enter username: "

To print the read input, we still use the built-in $REPLY variable:

$ echo $REPLY

read2

Limiting input.

Sometimes we want to limit the characters being read. For this, we use the -n option. For example, to limit the number of characters to six we write:

$ read -n 6 -p "enter characters

read3

Now after 6 characters, the read command returns since we have limited the input it takes.

Hiding input.

In Linux when entering a password in a password prompt, the password phrase is usually hidden for security purposes. To hide sensitive information such as passwords in shell scripts, we use the -s option:

$ read -s -p "Enter secret paraphrase: "

Not to print the entered text, we can echo it normally:

$ echo $REPLY

read4

We can also limit the length of the password by using the -n option as demonstrated earlier:

$ read -n 4 -s -p "Enter secret paraphrase: "

read5

In this case, once the paraphrase reaches 4, the read command returns, and the entered secret is saved in the $REPLY built-in variable.

Read with here strings.

Here strings can also be used with the read command. For example, to read input from a here string and assign it to variables, we write:

$ read var1 var2 var3 <<< "The read command with here strings"

In this case, we initialize variables 1 to 3 and assign the strings from the text to the variables:
Apart from the echo command, we can also use printf to print formatted output:

$ printf "$var1 \n$var2 \n$var3 \n"

read7

Notice from the output how the rest of the strings starting from the third are assigned to the last variable.

Arrays.

With the read command, we can use arrays to store input, for example, in a case where we expect varying inputs.

To assign inputs to an array instead of variable names, we use the -a option as follows:

$ read -a arr <<< "Testing arrays with the read command"
$ for i in "${arr[@]}"; do
> echo "$i"
> done

read8

In the example, the text is read using here strings and stored in an array named arr. To print out the contents of the array we loop through it using a for loop.

Read command timeout.

In some cases, we want to time the read command, that is, after a specified number of seconds if nothing is read, it returns. For this we use the -t option:

$ read -t 2

When the above command is executed, after 2 seconds if nothing is read it returns.

Internal field separator (IFS)

By default, the read command splits text into strings using one or more empty white spaces, newlines, tabs, etc. This is flexible, in that, we can specify our own delimiters, for example, to use hyphens, we write:

$ {
> IFS="-"
> read var1 var2 var3
> echo "[$var1] [$var2] [$var3]"
> }
Python-Java-Prolog-Haskell-Typescript

When the text is printed to standard output, it will be separated using a semi-colon as we have specified above.

read11

We can separate our read input using a hyphen, commas, or even empty whitespace. We can even specify more than two delimiters, for example:

$ {
> IFS="-|"
> read var1 var2 var3
> echo "[$var1] [$var2] [$var3]"
> }
Python-Java-Prolog|Haskell-Typescript

In the above example, we have used the | character as a delimiter.

Return values.

The read command returns 0 is successful, if not, the error code is usually 1 or greater than 1.

read12

In this case, we receive an exits code of 0 meaning no error was encountered.

Summary.

The read command is commonly used in shell scripting. We have learned how to limit input, hide input, use delimiters, store input in an array in case of varying inputs, read from here strings, use prompts with the read command, set timeouts, the Internal Field Separator, and read command return values.

References.

For other read command options, we can execute the command $ man read in Linux.

read command in Linux
Share this