read

input a line to the KornShell 

KornShell Built-in


SYNOPSIS

read [-prs] [-u[d]] [variable?prompt] [variable ...]


DESCRIPTION

When you call read without options, it reads one line from the standard input, breaks the line into fields and assigns the fields to each variable, in order.

To determine where to break the line into fields, read uses the built-in variable IFS (Internal Field Separator). Encountering any of the characters in IFS means the end of one field and the beginning of the next. The default value of IFS is blank, tab and newline.

In general, a single IFS character separates fields. For example, if IFS is colon (:), read considers the input a::b to have three fields: a, an empty field and b; however, if IFS contains blanks, tabs and/or escaped newlines, read considers a sequence of multiple blanks, tabs and/or escaped newlines to be a single field separator. For example, a b has two fields, even though there are several blanks between the a and b.

The nth variable in the command line is assigned the nth field. If there are more input fields than there are variables, the last variable is assigned all of the unassigned fields. If there are more variables than fields, the extra variables are assigned the null string ("").

The environment variable REPLY is assigned the input when no variables are given. The exit status of read is 0, unless it encounters end-of-file.

When the first variable parameter has the form

variable?prompt

it defines a prompt for input. If the shell is interactive, read sends the prompt to the file descriptor d if it is open for write and is a terminal device. The default file descriptor for the prompt is 2.

Options

-p 

receives input from a co-process. For a description of a co-process, see sh.

-r 

reads input in raw mode, ignoring escape conventions. For example, it does not interpret a final backslash (\) as a line continuation character, but as part of the input.

-s 

adds input to the command history file as well as to the variables.

-u [d

reads input from the single digit file descriptor d, rather than from the standard input. The default file descriptor is 0.


EXAMPLE

IFS=';'
while read name junk
do
	echo $name
done <ROOTDIR/etc/passwd

provides a list of users from the ROOTDIR/etc/passwd file.


ENVIRONMENT VARIABLES

IFS 

contains a string of characters to be used as internal field separators.

Note:

Using the unset on this environment variable does not remove it from the environment. It restores the variable to its default value of space, tab, and newline.

PS2 

contains the prompt string that an interactive shell uses when it reads a line ending with a backslash and you did not specify the -r option, or if a here-document is not terminated after you enter a newline.

REPLY 

contains the input (including separators) if you did not specify any variables.


DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

1 

Failure due to any of the following:

— end-of-file on input
— invalid variable
— incorrect descriptor specified after -u
— missing co-process
2 

Failure due to an invalid command line argument.

Cannot read on file descriptor ... 

You tried to read a file descriptor that was not opened for reading.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. Windows 7. Windows Server 2008 R2. Windows 8. Windows Server 2012. Windows 10. Windows Server 2016. read is a built-in command of the Bourne Shell and KornShell on UNIX systems. The Bourne Shell does not implement parameters of the form variable?prompt, or any options.

The -p, -s, and -u options are extensions to the POSIX standard.


NOTE

This command is built into the MKS KornShell.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC Integrity


SEE ALSO

Commands:
continue, fc, print, sh


PTC MKS Toolkit 10.1 Documentation Build 15.