Bash read Command | Explained

The Bash scripts are an essential automation tool for Linux users. The bash scripts can be used to create your customized functions, macros and schedule commands to several commands. To make the bash scripts interactive (take user input), the read command is very helpful. The user passes the commands as arguments to the Bash read command for executing the commands in a sequence.

This post will elaborate on the read command and its uses with the following timeline:

  • What is a read Command in Linux?
  • How to Use read Command in Bash?
  • Basic Use of the read Command
  • Example 1: Store the read Command Text in a Variable
  • Example 2: Store read Command input in Multiple Variables
  • Example 3: Use read Command as Prompt in Bash
  • Example 4: Use read Command to Hide Input Prompt in Bash
  • Example 5: Use read Command to Store Array Values
  • Example 6: Use Read Command to Set Delimiter
  • Example 7: Use Read Command to Set Character Limit
  • Example 8: Use Read Command to Set TimeOut
  • Example 9: Use Read Command With Loops

Let’s start with the basic understanding of the read command.

What is a read Command in Linux?

The read command allows the users to read the data from standard input (user input). It allows users to take input from the terminal, which makes the bash scripts interactive. The user can enter the desired input fields, passwords, loops, and functions using the read command.

The syntax for the read command is:

$ read [option] [arguments]

The built-in read command comes with several options, which are listed below:

aRead the array values.
dAllows the delimiter option to separate the values like -, _, etc.
pTakes the user input from the terminal.
N nIt is used to limit the characters in the standard input.
tSpecifies the time period in which the read command takes the input.
uReads from the file descriptor instead of standard input.
sHide the input data.

How to Use read Command in Bash?

The read command can be used in several ways to perform the desired function. This section will explain the usage of the read command with the help of examples.

Basic Use of the read Command

The read command takes the input from standard input. The entered text can be retrieved using the Reply variable, which stores the previous read command text, as shown below:

#!/bin/bash read echo "You entered: $REPLY"

Run this script by executing this command:

$ ~/run_script.sh

The word “Hello” is entered by the user and retrieved (echo) in the output using the $REPLY variable.

Example 1: Store the read Command Text in a Variable

If we write an argument with the read command, the input is stored in that argument. The argument value can be retrieved by using ‘$’ before that argument as performed below:

#!/bin/bash
read input
echo "The input is: $input"

Run the above bash script using this command:

$ ~/read_script.sh

The word “Test” is stored in the input argument and called with the echo command.

Example 2: Store read Command input in Multiple Variables

Several variables are used as arguments to store the value of the read command. For instance, two arguments, input1 and input2, are used below to store the read command input:

#!/bin/bash
read input1 input2
echo "The input1 is: $input1"
echo "The input1 is: $input2"

Execute the above bash command using this command:

$ ~/read_script.sh

The output shows that “Hello World!” is entered where “Hello” is stored as input1 and “World” is stored as input2 variable.

Example 3: Use read Command as Prompt in Bash

As the read command reads from the standard input, the read command can also be used with the “p” (default option) to take input from the user:

#!/bin/bash
read -p "Enter Your Name: " name
echo "Your name is: $name"

To check the output, utilize the below command:

$ ~/read_script.sh

The user enters the “itslinuxfoss” as shown in the output.

Example 4: Use read Command to Hide Input Prompt in Bash

The read command is used with the “s” option to hide the sensitive data. For instance, the below command uses the “s” option for the password, which needs to hide:

#!/bin/bash
read -p "Enter Your Name: " name
echo "Your name is: $name"
read -p "Enter your password: "$'\n' -s pass
echo "Thank you for entering your credentials!"

Execute the above script using the following command:

$ ~/read_script.sh

The output verifies that the password characters are hidden while the name is showing.

Example 5: Use read Command to Store Array Values

The array values can be read using the “a” option. The entire array values are stored in a single variable, or every index value for an array can be stored in a separate variable. For instance, the below three array values are used separately with their array index:

#!/bin/bash
read -a arr <<< "Linux Ubuntu Fedora"
echo "Array first value is: ${arr[0]}"
echo "Array second value is: ${arr[1]}"
echo "Array second value is: ${arr[2]}"

Use the following bash command to run the script:

$ bash read_script.sh

The output shows every index value of the array is displayed on a separate line.

Example 6: Use Read Command to Set Delimiter

If multiple variables are used with the read command, the blank space is used as the default delimiter for arguments.

In contrast, the “Enter” key is used as the delimiter in the terminal to exit the read command. But several other delimiters such as Comma, Dot, Dash, Under Score, and others can be used with the read command option “d” to exit the terminal.

When the user enters the delimiter, the read command exits the terminal and shows the standard input on the terminal. To use the Dot (.) as a delimiter, run this command in the terminal:

#!/bin/bash
read -d "."

For executing the above shell script, execute the below command:

$ bash read_script.sh

The output shows that the user enters the “Thank you.” and when the Dot (.) is used, the user exits the terminal.

Example 7: Use Read Command to Set Character Limit

The “n” or “N” options limit the number of characters in the read command standard input. To limit the read command to take only 5 characters, run this command:

#!/bin/bash
read -n 5 input
echo "The input is: $input"

For executing the bash script, use the following command in the terminal:

$ bash read_script.sh

Only five characters (12345) are entered, and the user automatically exits the standard input.

Example 8: Use Read Command to Set TimeOut

If you want to set a specific time limit in which a user can input with the read command, the “t” option is utilized. For instance, to allow the user to input within 10 seconds, use this bash script:

Note: The maximum read command limit is 10 seconds for this example, while the user can press the “Enter” key to exit the standard input before 10 seconds. If the user does not enter the input in the specified time (10 seconds), the user will automatically exit the terminal.

#!/bin/bash
read -t 10

To execute the above time limit bash script, run this command in the terminal:

$ bash read_script.sh

The user enters the word “Hello” and exits the terminal.

Example 9: Use Read Command With Loops

The read command can be used with the while, for, and if-else loops. The read command makes the loops more interactive, which depends on the user’s entered value. For instance, let’s run the desired command in bash based on user choice using this code:

#!/bin/bash

echo 1=Print Hostname
echo 2=Display Present Working Directory
echo 3=Print Today Date
echo    #It is used to insert empty line.

echo "Hi! Choose your preferred choice?"
read choice

case $choice in
1)hostname;;   
2)pwd;;
3)date;;
*)echo "Invalid Choice"

esac

Execute the bash script code for a while loop using the following command:

$ ~/read_script.sh

The output for the above script depends on the user’s choice; the user selects option “1”, which shows the hostname. While the output displays the current directory when the user chooses option “2”.

Conclusion

The read command takes the standard input (user input) from the terminal and then executes the commands the user enters. It can be used with arguments to store values, as a prompt to take hidden input, read array values, and set the characters & time limit. Moreover, the read command is used with loops like while, for, and if-else loops, as described in this article.