The Linux read command is a bash builtin that is typically used to accept user input in a shell script. You can assign that input to a variable to be used for processing. There is however, a lot more to the read command. In this article we will take an in-depth look at the read command, it's options and show you some examples of it's usage.
Table of Contents
Basic Usage of the Read Command
By default the read command will take input from stdin (standard input) and store it in a variable called $REPLY
. The default delimiter for read is a newline, so it will accept input until you hit the enter key.
Store Input in a Variable
Read always stored your input in a variable. As mentioned above it uses $REPLY
for the default variable. If you want to store the input into a user defined variable, you can simply invoke the read command with a single argument. Instead of the default $REPLY
variable, read will use the argument you supply. This comes in handy when writing a script that requires the user to input several different pieces of information.
Prompt User for Input
The read command comes with the -p
(prompt) option, which displays a prompt to allow user input. When using the prompt option it requires text as an argument. This can be a brief explanation of what kind of input you are expecting.
For example, if you were requesting that a user input their first name, it would like like this:
read -p "Please enter your first name: "
As mentioned above, the $REPLY
variable stores the input to the read command by default. If you are using this in a script and asking for multiple inputs, you can specify a user defined variable. For example, if you wanted to store the users first name in the FNAME
variable, simply add the variable name to the end of the command.
[savona@putor ~]$ read -p "Please enter your first name: " FNAME Please enter your first name: Steve [savona@putor ~]$ echo $FNAME Steve
Accepting Silent Input for Sensitive Data
You can use the read commands -s
(silent mode) option to allow people to enter sensitive data. This DOES NOT secure your information in anyway other than hiding the input on the terminal. Always take special considerations when dealing with passwords in plain text.
With that being said, passing the -s
option will stop the characters from being echoed to stdout (standard output).
You can use -s
(silent) as a lone option, you can pair it with the -p
(prompt) option and you can also specify your own variable to store it in.
Limiting User Input to x Number of Characters
You can limit the number of characters entered by using the -n
(nchars) option. If you reach the number specified it will automatically stop reading input.
As you can see in the example above, I attempted to input "okaydokay" but it automatically stopped when I hit 5 characters.
Additionally, you can use this in conjunction with the -p
(prompt) option as well using a user specified variable.
Input with Backslash
By default the backslash acts as an escape character. If you have a backslash in your input and want it to be part of the line, you can use the -r
option.
In this example we first use the backslash ( \ ) then enter (escaping the new line) as a line continuation and read accepts the rest of the input. Second, we use the -r
option which takes this functionality away and uses the backslash as a literal character, not it's special meaning. So when we enter backslash + enter, it ends the input.
Advanced Usage of the read Command
In this section we will show you some more advanced uses for the Linux read command. We will touch on many concepts that will help with development of scripts.
Setting a Delimiter for Input
A newline terminates the input by default. We can set this to whatever we like. For example, we can set it to a pound/hash sign (#).
Setting a Timeout for User Input
By default read will just sit and wait for the user input. If you want to set a time limit on how long the user has to finish their input you can use the -t
option. The timeout can be set in seconds, and if it expires without input it returns failure. In the following example we set the timeout for 5 seconds. The echo command shows the non-zero exit code, which indicates failure.
Creating an Array from User Input
An array holds several values, whereas a variable only holds a single value. The single value in a variable can have multiple words, but it is still treated as a single value. An array stores multiple values separately.
To store the input into an array, we can use the -a
(aname) option followed by the name of the array, like so:
[savona@putor ~]$ read -a animals
This will take the input and split it into the array. Each word from the input is assigned as a value in the array, starting with 0. We can enter the values for the array separated by a space, hitting the enter key to denote the end of input.
Here is an example of putting names of animals into the ANIMALS array:
[savona@putor ~]$ read -a ANIMALS dog cat cow horse turtle
Now let's try to echo ANIMALS like we did earlier.
[savona@putor ~]$ echo $animals dog
It only printed "dog" which is the first value in the array. This is because unlike using a variable, the values in an array were split at the spaces. In order to print all the values in the ANIMAL, we have to let bash know we are using an array, not a variable.
[savona@putor ~]$ echo ${animals[@]} dog cat cow horse turtle
So dog is 0 in the animals array, cat is 1, and so on. We can print whichever value we like from the array using these numeric assignments.
[savona@putor ~]$ echo ${animals[0]} dog [savona@putor ~]$ echo ${animals[2]} cow [savona@putor ~]$ echo ${animals[4]} turtle
NOTE: The full use of arrays is outside the scope of this tutorial.
Rare Options of the read Command
There are a couple options that are rarely used, but we will touch on them for completeness.
Using Readline Libraries to Obtain Input
Using the -e
option allows you to use readline editing in the input. The readline provides functionality to allow your to move the cursor, delete words, delete from cursor to end of line, and a lot more.
Often during an interactive session you type in a long line of text, only to notice that the first word on the line is misspelled. The Readline library gives you a set of commands for manipulating the text as you type it in, allowing you to just fix your typo, and not forcing you to retype the majority of the line.
Gnu.org
Explaining readline is outside the scope of this tutorial, you can find links to more information below in the resources section.
Insert Text into Editing Buffer if using Readline
If you are using Readline ( -e
) then passing the -i
option will insert text into the editing buffer. Here is an example:
Conclusion
I think we covered everything there is to cover about the read command. We discussed storing input in variables, prompting for user input, creating arrays and a lot more. The read command is a great tool for all you DevOp folks out there.
Sound off in the comments below and let me know how you use the read command.
Resources and Links
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
3 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
- Uncategorized (1)
read -t 5 -p "Enter your inputs here"
-t option used here to define a time limit of 5 seconds.
Thanks! I have updated the article with the timeout feature.
Hey Steve, thoroughly enjoyed your article on READ command. I was kind of mesmerized by the terminal window animation (correct me if it's gif or something else). It was like executing the command myself on the command line. Wondering how did you do the magic?