read(1) read(1)
NAME
read - read arguments from standard input and assign to shell vari-
ables
SYNOPSIS
read name ...
DESCRIPTION
read is a shell built-in command that reads a line from standard input
and sequentially assigns the individual input line arguments as values
to the shell variables specified in the call.
The only argument separators that read recognizes are the characters
assigned to the IFS shell variable, the defaults being blanks, tabs
and newline characters.
If read appears in a shell script and standard input has not been
redirected, the script halts execution in order to read your next
input from standard input. The script resumes execution as soon as you
enter a newline character (see EXAMPLES).
OPERANDS
The Korn shell ksh supports additional options [see ksh(1)].
name Name of the shell variable to which the corresponding input line
argument is assigned. The first argument is assigned to the first
name, the second argument goes to the second, and so on, with the
last name assigned whatever remains on the input line.
The names of shell variables must start with a letter or an
underscore (_) and must consist of letters, underscores and
digits only.
Any leftover arguments in the input line are assigned to the last
variable specified in the read command line.
Excess variables are assigned the null string.
EXIT STATUS
0 when read executes successfully
>0 when no input is received, i.e. EOF is encountered.
ERROR MESSAGES
text: not an identifier
This error message may have the following causes:
- either you did not specify a variable name on the command line, or
- the name you specified contains illegal characters.
Page 1 Reliant UNIX 5.44 Printed 11/98
read(1) read(1)
read: missing arguments
You called read without arguments.
ENVIRONMENT VARIABLES
IFS Input field separator (argument delimiter). The default values
are blank, tab and newline.
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
EXAMPLES
Example 1
The read command is invoked in a script named readtest, which contains
the following:
: Invoked with sh readtest, halts for input
echo Please enter a customer name:
read customer1 customer2 customer3
if [ -z "$customer1" ]
then exit 5
else echo Customer1: $customer1
echo Customer2: $customer2
echo Customer3: $customer3
fi
Invocation of the readtest script file:
$ sh readtest
Please enter a customer name:
Shaw Bowden Pitman Potter
Customer1: Shaw
Customer2: Bowden
Customer3: Pitman Potter
After invocation, the shell script issues the message specified in the
echo command and invokes read. The script halts, and the entered cus-
tomer names are then read in.
The newline character terminates the input line for read. The third
variable customer3 is assigned two names, since four arguments were
specified in the input line.
Page 2 Reliant UNIX 5.44 Printed 11/98
read(1) read(1)
Example 2
Use of the read command to read in the first line from a file:
$ read line < /etc/group
$ echo $line
root::0:root
In this case, the first line of the file /etc/group will always be
read, even if read is invoked repeatedly.
Example 3
The following shell script makes use of the read command in order to
read in lines from a file successively:
: Invoked with sh readinall
exec < /etc/group
for i in 1 2 3 4 5 6 7
do
read record$i
eval echo record$i: \$record$i
done
In the shell script readinall, the shell built-in exec redirects the
standard input to the file /etc/group for the following read command.
Owing to the for loop, read is invoked seven times in the script. Each
invocation positions the read pointer on the next line, thus causing
echo to output the first seven lines of the /etc/group file in succes-
sion:
$ sh readinall
record1: root::0:root
record2: daemon::1:daemon
record3: sys::2:sys:
record4: bin::3:bin,admin
record5: uucp::4:
record6: ces::5:
record7: other::10:gast,mgast,tele
To evaluate the argument \$record$i correctly, the shell has to inter-
pret the echo command line twice; hence the inclusion of eval. At the
first attempt the shell only interprets $i, as the first dollar sign
is escaped by the backslash. At the second attempt the shell inter-
prets $record[1-7].
NOTES
Some differences in behavior may occur when using read, depending on
which shell is being used. The possible differences are not described
specifically.
Page 3 Reliant UNIX 5.44 Printed 11/98
read(1) read(1)
SEE ALSO
exec(1), ksh(1), sh(1).
Page 4 Reliant UNIX 5.44 Printed 11/98