How To Use The Bash read Command?


The read command is one of the most fundamental commands in Bash scripting. It is used to read input from the user or from a file. In this article, we will explore how to use the read command effectively, with several examples and their output.

Basic Usage of read Command

The most basic usage of the read command is to take input from the user. Here's a simple example −

Example

echo "Please enter your name: "
read name
echo "Hello, $name"

When you run this script, it will prompt you to enter your name. After you enter your name, it will greet you with a message. Here's an example of the output −

Output

Please enter your name: 
John
Hello, John

Reading Multiple Inputs

The read command can also read multiple inputs at once. Here's an example −

Example

echo "Enter your first and last name: "
read first_name last_name
echo "Hello, $first_name $last_name"

In this script, the read command reads two inputs and assigns them to first_name and last_name respectively. Here's an example of the output −

Output

Enter your first and last name: 
John Doe
Hello, John Doe

Using a Prompt with read Command

You can also use the -p option with the read command to display a prompt before reading the input. Here's an example −

Example

read -p "Enter your name: " name
echo "Hello, $name"

In this script, the read command displays the prompt "Enter your name: " and then reads the input. Here's an example of the output −

Output

Enter your name: John
Hello, John

Reading Input from a File

The read command can also be used to read input from a file. Here's an example −

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

In this script, the read command reads each line from the file file.txt and the echo command prints each line. The output will be the contents of file.txt.

Reading Input with a Timeout

The read command can also be used with a timeout. If the user does not provide an input within the specified time, the script will continue. Here's an example −

read -p "You have 5 seconds to enter your name: " -t 5 name
echo "Hello, $name"

In this script, if the user does not enter their name within 5 seconds, the script will continue and print "Hello, " without a name.

Reading Input into an Array

The -a option allows you to read input into an array. Here's an example −

Example

echo "Enter three numbers separated by space: "
read -a numbers
echo "The numbers are: ${numbers[0]}, ${numbers[1]}, ${numbers[2]}"

In this script, the read command reads three inputs and assigns them to an array numbers. Here's an example of the output −

Output

Enter three numbers separated by space: 
10 20 30
The numbers are: 10, 20, 30

Reading Input without Backslash Interpretation

The -r option allows you to read input without backslash interpretation. Here's an example −

Example

read -r -p "Enter a string: " str
echo "You entered: $str"

In this script, if you enter a string with a backslash, the read command will not interpret the backslash. Here's an example of the output −

Output

Enter a string: Hello\ World
You entered: Hello\ World

Without the -r option, the backslash would be interpreted and not printed −

Example

read -p "Enter a string: " str
echo "You entered: $str"

Output

Enter a string: Hello\ World
You entered: Hello World

Reading Input with a Delimiter

The -d option allows you to specify a delimiter to terminate the input. Here's an example −

Example

read -d ';' -p "Enter a string (end with ;): " str
echo "You entered: $str"

In this script, the read command will keep reading input until it encounters a semicolon. Here's an example of the output:

Output

Enter a string (end with ;): Hello World;
You entered: Hello World

Silent Mode

The -s option allows you to read input in silent mode, which is useful when you're asking for sensitive information like passwords. Here's an example −

Example

read -sp "Enter your password: " password
echo -e "
Your password length is: ${#password}"

In this script, the read command reads the password without displaying it on the screen. The length of the password is then printed. Here's an example of the output −

Output

Enter your password: 
Your password length is: 8

Note − The actual password is not displayed when you type it in.

Reading from a File Line by Line

You can also use the read command to read from a file line by line and perform operations on each line. Here's an example −

line_number=1
while read -r line
do
   echo "Line $line_number: $line"
   ((line_number++))
done < file.txt

In this script, the read command reads each line from the file file.txt and the echo command prints the line number and the line. The output will depend on the contents of file.txt.

Reading Input with a Default Value

The -i option allows you to specify a default value that will be used if the user does not provide an input. Here's an example −

Example

read -p "Enter your name: " -i "John Doe" name
echo "Hello, $name"

In this script, if the user does not enter a name, the script will use "John Doe" as the default value. Here's an example of the output −

Output

Enter your name: 
Hello, John Doe

Reading Input into Associative Array

The -A option allows you to read input into an associative array. Here's an example −

Example

declare -A person
read -p "Enter your name and age: " -a person
echo "Hello, ${person[0]}, you are ${person[1]} years old."

In this script, the read command reads two inputs and assigns them to an associative array person. Here's an example of the output −

Output

Enter your name and age: 
John 25
Hello, John, you are 25 years old.

These examples should give you a good understanding of how to use the read command in Bash scripting. The read command is very versatile and can be used in many different ways to make your scripts more interactive and user-friendly.

Conclusion

The read command is a versatile tool in Bash scripting. It can be used to read input from the user or from a file, and it has several options that allow you to customize its behavior. By understanding how to use the read command effectively, you can make your Bash scripts more interactive and user-friendly.

Updated on: 13-Jul-2023

937 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements