DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

read(C)


read -- read a line from standard input

Syntax

read [ -r ] variable ...

Description

read reads a single line from standard input. The line is split into fields separated by the internal field separator (IFS). Each variable is an existing or nonexisting shell variable.

If there are fewer variable arguments than standard input fields then the leftover fields are assigned to the final variable. If there are fewer fields than variables then the remaining variables are set to empty strings.

The -r option causes the backslash character ``\'' to be interpreted as part of the input line. By default backslash is treated as a line continuation character.

Examples

Note that these examples are for the Korn shell only.

Print input_file with the first field of each line moved to the end of the line:

   while read -r xx yy
   do
   	printf "%s %s\n" "$yy" "$xx"
   done < input_file
Print out the user ID of each user in /etc/passwd:
   IFS=":"
   

while true do read -r uname passwd uid remainder || exit print "${uname}'s UID is $uid" done < /etc/passwd

Limitations

read affects the current shell environment only. Values of variables read in a subshell are not exported to the calling shell. For example (read $X) will not change the value of the variable X in the current shell.

read is implemented as a built-in command to the Bourne and Korn Shells only. It is not available in the C shell.

See also

ksh(C), printf(C)

Standards conformance

read is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003