Bash read Command

Introduction

The read command is a built-in command in Bash that allows users to read input from the keyboard and assign it to variables. It is commonly used to interactively prompt the user for input within a Bash script.

In this tutorial, we’ll explore the built-in read command. We will also address a few FAQs on Bash read command.

Bash read Built-in

To parse a line from the standard input (or the file descriptor) into individual words, you may use the read command, which is built into bash. In this system, the first word represents the first name, the second represents the second name, and so on.

The read built-general in's syntax is expressed as follows:

read [options] [name...]

To see an example of this command in action, open a terminal and enter read var1 var2. Any time the user changes the input, the command will pause for input. Enter after each word you type.

read var1 var2
Hello, World!

The names supplied as parameters to the read command become associated with the words. For testing purposes, you may use echo or printf:

echo $var1
echo $var2
Output

Hello,
World!

The standard input may be sent to read by ways other than typing at the terminal, such as piping, here-string, or a heredoc.

echo "Hello, World!" | (read var1 var2; echo -e "$var1 \n$var2")
ℹ️
read and echo are enclosed in parentheses and executed in the same subshell.

In the same subshell, read and echo are both performed with parenthesis around them.

Output

Hello,
World!

Here is an example using printf and a here-string:

read -r var1 var2 <<< "Hello, World!"
printf "var1: %s \nvar2: %s\n" "$var1" "$var2"
Output

Var1: Hello, 
Var2: World! 

The full line is allocated to the REPLY variable when the read command is issued without an argument:

echo "Hello, world!" | (read; echo "$REPLY")
Output

Hello, World!

Words not used in the first name are added to the last name if the number of parameters passed to read is more than the number of words read.

echo "Linux is awesome." | (read var1 var2; echo -e "Var1: $var1 \nVar2: $var2")
Output

Var1: Linux 
Var2: is awesome.

Otherwise, an empty value is given to the remaining names if the number of arguments is fewer than the number of names:

echo "Hello, World!" | (read var1 var2 var3; echo -e "Var1: $var1 \nVar2: $var2 \nVar3: $var3")
Output

Var1: Hello, 
Var2: World! 
Var3: 

Because read treats the backslash as an escape character by default, it might lead to surprising results in certain cases. By using the -r option, you may prevent the command from escaping backslashes.

Following is an illustration of the difference between invoking read with and without the -r option:

read <<< "Hello, \tWorld!"
printf %s "$REPLY"
Output

Hello, tWorld!
read -r <<< "Hello, \tWorld!"
printf %s "$REPLY"
Output

Hello, \tWorld!

In general, you must use read with the -r option.

Changing the Delimiter

By default, read will divide the line into words using any combination of whitespace, tabs, and newlines. Set the IFS variable to a different character if you'd want to utilize it as a separator (Internal Field Separator).

echo "Linux:is:awesome." | (IFS=":" read -r var1 var2 var3; echo -e "$var1 \n$var2 \n$var3")
Output

Linux 
is 
awesome.

The words are separated by precisely one character when IFS is set to a character other than a space or tab:

echo "Linux::is:awesome." | \
  (IFS=":" read -r var1 var2 var3 var4; echo -e "Var1: $var1 \nVar2: $var2 \nVar3: $var3 \nVar4: $var4")

There are four words between each line. The second term represents the space in between the commas as a value of zero. When we put two delimiter characters close to each other, it produces this (::).

Output

Var1: Linux 
Var2:  
Var3: is 
Var4: awesome.

Line breaks may be made using many different characters. When assigning characters to the IFS variable for use as multiple delimiters, no space should be included between each character.

The delimiters   _ an - as are seen in this example:

echo 'Linux_is-awesome.' | (IFS="-_" read -r var1 var2 var3; echo -e "$var1 \n$var2 \n$var3")
Output

Linux 
is 
awesome.

Prompt String

The read command is useful for gathering user input in interactive bash programs.

The -p option allows the user to provide a custom prompt string. The read prompt is shown before the command is run, and it does not include a newline.

Here is an easy illustration:

read -r -p "Are you sure?"

The read command is often used inside a while loop to coerce the user into providing one of many predetermined responses.

This code will request the user to restart the machine.

while true; do
    read -r -p "Do you wish to reboot the system? (Y/N): " answer
    case $answer in
        [Yy]* ) reboot; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer Y or N.";;
    esac
done

Use the -s option to instruct read not to display the input on the terminal if the shell script asks users to provide sensitive information, such as a password:

read -r -s -p "Enter your password: "

Assign the Words to Array

Use the read command with the -a option to assign the words to an array rather than variable names:

read -r -a MY_ARR <<< "Linux is awesome."

for i in "${MY_ARR[@]}"; do 
  echo "$i"
done
Output


Linux 
is 
awesome.

All words are allocated to the array when both an array and a variable name are specified.

FAQs to Bash Read Command

How can I use the read command in a Bash script? 

To use the read command, specify the name of the variable you want to assign the input value to, like read variable_name. The user input will be assigned to the variable.

Can I prompt the user with a message when using the read command? 

Yes, you can provide a prompt to the user by using the -p option followed by the message you want to display, like read -p "Enter your name: " name.

What happens if the user enters nothing when prompted by read? 

If the user enters nothing and simply presses Enter, the variable assigned by read will hold an empty value.

Can read be used to read multiple values in a single command? 

Yes, you can read multiple values using read by specifying multiple variables separated by spaces, like read var1 var2 var3.

How can I read a line with spaces or preserve leading/trailing whitespace? 

By default, read trims leading and trailing whitespace and treats consecutive whitespace as a delimiter. To preserve whitespace and read a whole line, use the -r option, like read -r line.

Can read be used to read input from a file instead of the keyboard? 

No, the read command is specifically designed to read input from the keyboard. To read input from a file, you can redirect the file as input to the script or use commands like while read line.

Can read accept a default value for a variable if no input is provided? 

Yes, you can assign a default value by using the -e option followed by the desired default value. If no input is provided, the default value will be assigned to the variable.

Conclusion

To break up a line of data into words, use the read command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.