Linux tip: Using the read command

If you write shell scripts, this is one cool builtin Linux command you should know.

$ cat read1_no_quote read -p "Go ahead: " firstline echo You entered: $firstline $ ./read1_no_quote Go ahead: * You entered: read1 read1_no_quote script.1 $ ls read1     read1_no_quote     script.1


command lists the same words as the script, demonstrating that the shell expands the asterisk into a list of files in the working directory. When the variable $firstline is surrounded by double quotation marks, the shell does not expand the asterisk. Thus the read1 script behaves correctly:

$ ./read1 Go ahead: * You entered: *


When you do not specify a variable to receive

's input,
puts the input into the variable named REPLY. The following read1b script performs the same task as read1:

$ cat read1b read -p "Go ahead: " echo "You entered: $REPLY"

The read2 script prompts for a command line, reads the user's response, and assigns it to the variable cmd. The script then attempts to execute the command line that results from the expansion of the cmd variable:

$ cat read2 read -p "Enter a command: " cmd $cmd echo "Thanks"

In the following example, read2 reads a command line that calls the

builtin. The shell executes the command and then displays Thanks. Next read2 reads a command line that executes the

$ ./read2 Enter a command: echo Please display this message. Please display this message. Thanks $ ./read2 Enter a command: who max     pts/4      2013-06-17 07:50 (:0.0) sam     pts/12     2013-06-17 11:54 (guava) Thanks

If cmd does not expand into a valid command line, the shell issues an error message:

$ ./read2 Enter a command: xxx ./read2: line 2: xxx: command not found Thanks

The read3 script reads values into three variables. The

builtin assigns one word (a sequence of nonblank characters) to each variable:

$ cat read3 read -p "Enter something: " word1 word2 word3 echo "Word 1 is: $word1" echo "Word 2 is: $word2" echo "Word 3 is: $word3" $ ./read3 Enter something: this is something Word 1 is: this Word 2 is: is Word 3 is: something

When you enter more words than

has variables,
assigns one word to each variable, assigning all leftover words to the last variable. Both read1 and read2 assigned the first word and all leftover words to the one variable the scripts each had to work with. In the following example,
assigns five words to three variables: It assigns the first word to the first variable, the second word to the second variable, and the third through fifth words to the third variable.

$ ./read3 Enter something: this is something else, really. Word 1 is: this Word 2 is: is Word 3 is: something else, really.

Table 10-4 lists some of the options supported by the read builtin.


builtin returns an exit status of 0 if it successfully reads any data. It has a nonzero exit status when it reaches the EOF (end of file).

The following example runs a while loop from the command line. It takes its input from the names file and terminates after reading the last line from names.

$ cat names Alice Jones Robert Smith Alice Paulson John Q. Public $ while read first rest > do > echo $rest, $first > done < names Jones, Alice Smith, Robert Paulson, Alice Q. Public, John $

The placement of the redirection symbol (

) for the while structure is critical. It is important that you place the redirection symbol at the done statement and not at the call to

| 1 2 3 Page 4
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon