Beginners Guide for Read Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The read command is a built-in Linux utility for shell script writers (and for you) to take single line input from the keyboard or from the file descriptor and store it in a variable.

The applications for this command are wide ranging, like asking for user input in plain or private text, specifying the idle wait time for user input, using a custom internal field separator, etc.

Tutorial Details

DescriptionRead
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesread
Internet RequiredNo

Syntax of the Read Command

The read command takes two arguments: one is the option, and the other is the name.

$ read [OPTION] [NAME...]

Taking User Input to a Single Variable Name

When you execute the read command, with or without an option or name, it will ask for user input, and typed input values will be stored in the β€œ$REPLY” environment variable.

Taking user input using the read command without an option or name

If you specify the variable name, all the typed user input values will be stored in that specified variable instead of the β€œ$REPLY” variable.

Taking user input using the read command with a variable name

So, let’s learn more about the read command, including its options.

Splitting User Input Values into Multiple Variable Names

As you have just seen in the above example, when you specify a single variable name, all the user input values will be stored in that single variable.

However, if you specify multiple variable names, it will use the space as a splitter and store each single input value to each variable, and remaining values will be stored to the last variable name.

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

Output:

Splitting user input values into multiple variable names

Including spaces (tab and newline) will also be treated as separators.

Tab and newline are also separators

Specifying a Custom Field Separator

To change the default behavior of the read command taking a space, tab, or newline as a separator, specify your own custom separator using the IFS (input field separator) variable, as shown.

$ IFS=";" read var1 var2
$ echo var1
$ echo var2

Output:

Using a custom separator for the read command

Displaying a Prompt Before the Input

Most of the time, you will ask for user input with specific intentions in the script, like asking for name, username, password, etc.

Display a prompt before the input field using the β€œ-p” flag to make it easier for the user to understand the intention of the input field.

$ read -p "Enter your name: " var1
$ echo $var1

Output:

Displaying a Prompt Before the Input

Asking for Secret Input from the User

Name or age is not the only thing you will ever ask from the user; it can also be sensitive data like a password or pin that should be kept secret and hidden while typing.

To achieve this, you can use the β€œ-s” flag to hide the user input from the screen while typing.

$ read -s -p "Enter your password: " var1
$ echo $var1

Output:

Hiding the user's input while typing

Limiting the User Input Length

Input fields without limiting the number of characters will always be disastrous for any program or script, which is why each input field should be restricted to a specific number of characters.

For example, you can use the β€œ-n” flag with an integer that will be used as the maximum number of characters allowed for that specific input field.

$ read -n 8 -p "Enter less than 8 character only: " var1
$ echo $var1

Output:

Limiting the length of characters for the input field

When you reach your eighth character, the input field will automatically terminate.

Specifying a Specific Character as a Delimiter Instead of a New Line

You usually press the β€œenter” key when you are done typing (acting as a delimiter), but instead of that, you can specify a specific character or number that will be used as a delimiter using the β€œ-d” flag.

Assigning a custom delimiter for the read command

Specifying the Idle Wait Time for User Input

You can use the β€œ-t” flag with an integer (in seconds) to wait for the user’s input. After the specific timeout is reached, the input field will terminate itself with an error.

$ read -t 5 -p "Enter within 5 seconds: " var1
$ echo $var1

Output:

Specifying the idle wait time for user input

Exit Status

The read command has the following exit statuses that can be used to tackle errors while working on shell scripts:

  • 0: Successful
  • 2: An invalid option is provided.
  • 142: Input was not provided before the timeout.

Read Command Implementation in Shell Script

Learning about the read command and its option might not be enough to help you understand its implementation in shell script.

For that, I’ve provided you with two examples of read command implementation in shell script.

Saving the User’s Input in a Text File [Easy]

The following shell script will ask for the username and password (whose length should be less than 10 characters) and stay idle for 30 seconds until the user passes the value; otherwise, it will terminate itself.

And the entered data will be saved to the β€œfile.txt” file in your present directory.

#! /bin/bash

read -t 30 -n 10 -p "Enter your username: " var1
read -t 30 -n 10 -s -p "Enter your password: " var2
echo ""
echo -e "Username: $var1 \nPassword: $var2" > file.txt

Output:

Read the command in shell script example one

Steps break down:

  1. The content of the script (saved with a β€œscript.sh” filename).
  2. Assigning executable permission to the script using the chmod command.
  3. Executing the script from its relative path.
  4. Reading the saved content in the β€œfile.txt” file.

Saving the User’s Input in a Text File [Tricky]

The following script is identical to the previous one with a few changes.

  • Instead of newline, β€œ$” will be used as a delimiter.
  • Use β€œ;” as separator instead of space.
  • Idle time out is 30 seconds.
  • Username and password will be taken using the array.
  • Input field length is 20 characters for each field.
  • Print and save the output in the β€œfile.txt” file.
#! /bin/bash

echo "
#########################################
Enter your username and password in 
less 10 character using ';' as separator 
and press '$' when you are done
########################################
"

IFS=";"

read -t 30 -n 20 -d "$" -p "Enter your username;password: " var1 var2

echo -e "\nYour username is" $var1
echo -e "Your password is" $var2

echo -e "Username: $var1 \nPassword: $var2" > file.txt

Output:

Read command in shell script example two

Steps break down:

  1. The content of the script (saved with a β€œscript.sh” filename).
  2. Giving an executable permission.
  3. Executing the script.
  4. Output of typed results.
  5. Reading the file with saved results.

As you can see in the array method, taking sensitive data is not worth it because you can’t use the β€œ-s” flag for a specific input field; instead, use separate fields for sensitive data.

So, that is all for now.

If you have any questions or queries related to this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.