< Previous | Contents | Next >
read – Read Values From Standard Input
The read builtin command is used to read a single line of standard input. This command can be used to read keyboard input or, when redirection is employed, a line of data from a file. The command has the following syntax:
read [-options] [variable...]
where options is one or more of the available options listed below and variable is the name of one or more variables used to hold the input value. If no variable name is sup- plied, the shell variable REPLY contains the line of data.
Basically, read assigns fields from standard input to the specified variables. If we mod- ify our integer evaluation script to use read, it might look like this:
#!/bin/bash
# read-integer: evaluate the value of an integer.
echo -n "Please enter an integer -> " read int
if [[ "$int" =~ ^-?[0-9]+$ ]]; then if [ $int -eq 0 ]; then
echo "$int is zero."
else
if [ $int -lt 0 ]; then
echo "$int is negative."
else
echo "$int is positive."
fi
if [ $((int % 2)) -eq 0 ]; then echo "$int is even."
else
echo "$int is odd."
fi
fi else
echo "Input value is not an integer." >&2 exit 1
fi
#!/bin/bash
# read-integer: evaluate the value of an integer.
echo -n "Please enter an integer -> " read int
if [[ "$int" =~ ^-?[0-9]+$ ]]; then if [ $int -eq 0 ]; then
echo "$int is zero."
else
if [ $int -lt 0 ]; then
echo "$int is negative."
else
echo "$int is positive."
fi
if [ $((int % 2)) -eq 0 ]; then echo "$int is even."
else
echo "$int is odd."
fi
fi else
echo "Input value is not an integer." >&2 exit 1
fi
We use echo with the -n option (which suppresses the trailing newline on output) to display a prompt, and then use read to input a value for the variable int. Running this script results in this:
[me@linuxbox ~]$ read-integer
Please enter an integer -> 5
5 is positive.
5 is odd.
[me@linuxbox ~]$ read-integer
Please enter an integer -> 5
5 is positive.
5 is odd.
read can assign input to multiple variables, as shown in this script:
#!/bin/bash
# read-multiple: read multiple values from keyboard echo -n "Enter one or more values > "
read var1 var2 var3 var4 var5
echo "var1 = '$var1'" echo "var2 = '$var2'" echo "var3 = '$var3'" echo "var4 = '$var4'" echo "var5 = '$var5'"
#!/bin/bash
# read-multiple: read multiple values from keyboard echo -n "Enter one or more values > "
read var1 var2 var3 var4 var5
echo "var1 = '$var1'" echo "var2 = '$var2'" echo "var3 = '$var3'" echo "var4 = '$var4'" echo "var5 = '$var5'"
In this script, we assign and display up to five values. Notice how read behaves when given different numbers of values:
[me@linuxbox ~]$ read-multiple
Enter one or more values > a b c d e
var1 = 'a' var2 = 'b' var3 = 'c' var4 = 'd' var5 = 'e'
[me@linuxbox ~]$ read-multiple Enter one or more values > a var1 = 'a'
var2 = '' var3 = '' var4 = '' var5 = ''
[me@linuxbox ~]$ read-multiple
Enter one or more values > a b c d e f g
var1 = 'a' var2 = 'b' var3 = 'c' var4 = 'd' var5 = 'e f g'
[me@linuxbox ~]$ read-multiple
Enter one or more values > a b c d e
var1 = 'a' var2 = 'b' var3 = 'c' var4 = 'd' var5 = 'e'
[me@linuxbox ~]$ read-multiple Enter one or more values > a var1 = 'a'
var2 = '' var3 = '' var4 = '' var5 = ''
[me@linuxbox ~]$ read-multiple
Enter one or more values > a b c d e f g
var1 = 'a' var2 = 'b' var3 = 'c' var4 = 'd' var5 = 'e f g'
If read receives fewer than the expected number, the extra variables are empty, while an excessive amount of input results in the final variable containing all of the extra input.
If no variables are listed after the read command, a shell variable, REPLY, will be as- signed all the input:
#!/bin/bash
# read-single: read multiple values into default variable echo -n "Enter one or more values > "
read
echo "REPLY = '$REPLY'"
#!/bin/bash
# read-single: read multiple values into default variable echo -n "Enter one or more values > "
read
echo "REPLY = '$REPLY'"
Running this script results in this:
[me@linuxbox ~]$ read-single
Enter one or more values > a b c d
REPLY = 'a b c d'
[me@linuxbox ~]$ read-single
Enter one or more values > a b c d
REPLY = 'a b c d'