Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
Home Bash Guide for Beginners Writing interactive scripts User Input Using the read built-in command | ||||||||||||||||||||||
See also: Input/output redirection, Prompting for user input | ||||||||||||||||||||||
Search the VIAS Library | Index | ||||||||||||||||||||||
Using the read built-in commandThe read built-in command is the counterpart of the echo and printf commands. The syntax of the read command is as follows: read [options] NAME1 NAME2 ... NAMEN One line is read from the standard input, or from the file descriptor supplied as an argument to the -u option. The first word of the line is assigned to the first name, NAME1, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name, NAMEN. If there are fewer words read from the input stream than there are names, the remaining names are assigned empty values. The characters in the value of the IFS variable are used to split the input line into words or tokens; see Section 3.4.8. The backslash character may be used to remove any special meaning for the next character read and for line continuation. If no names are supplied, the line read is assigned to the variable REPLY. The return code of the read command is zero, unless an end-of-file character is encountered, if read times out or if an invalid file descriptor is supplied as the argument to the -u option. The following options are supported by the Bash read built-in: Table 8-2. Options to the read built-in
This is a straightforward example, improving on the leaptest.sh script from the previous chapter:
|
||||||||||||||||||||||
Home Bash Guide for Beginners Writing interactive scripts User Input Using the read built-in command |