[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 4

read Command

Purpose

Reads one line from standard input.

Syntax

read [ -p ][  -r ][ -s ][ -u[ n ] ] [  VariableName?Prompt ]

[ VariableName ... ]

Description

The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators. The VariableName parameter specifies the name of a shell variable that takes the value of one field from the line of input. The first shell variable specified by the VariableName parameter is assigned the value of the first field, the second shell variable specified by the VariableName parameter is assigned the value of the second field, and so on, until the last field is reached. If the line of standard input has more fields than there are corresponding shell variables specified by the VariableName parameter, the last shell variable specified is given the value of all the remaining fields. If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.

Note: If you omit the VariableName parameter, the variable REPLY is used as the default variable name.

The setting of shell variables by the read command affects the current shell execution environment.

Flags

-p Reads input from the output of a process run by the Korn Shell using |& (pipe, ampersand).

Note: An end-of-file character with the -p flag causes cleanup for this process to so that another can be spawned.
-r Specifies that the read command treat a \ (backslash) character as part of the input line, not as a control character.
-s Saves the input as a command in the Korn Shell history file.
-u [ n ] Reads input from the one-digit file descriptor number, n. The file descriptor can be opened with the ksh exec built-in command. The default value of the n is 0, which refers to the keyboard. A value of 2 refers to standard error.

Parameters

VariableName?Prompt specifies the name of one variable, and a prompt to be used. When the Korn Shell is interactive, it will write the prompt to standard error, and then perform the input. If Prompt contains more than one word, you must enclose it in single or double quotes.
VariableName... specfies one or more variable names separated by white space.

Exit Status

This command returns the following exit values:

0 Successful completion.
>0 Detected end-of-file character or an error occurred.

Examples

  1. The following script prints a file with the first field of each line moved to the end of the line:

    while read -r xx yy
    do
             print printf "%s %s/n" $yy $xx
    done < InputFile
  2. To read a line and split it into fields, and use "Please enter: " as a prompt, type:

    read word1?"Please enter:  " word2

    The system displays:

    Please enter:
    You enter:
    hello world

    The value of the word1 variable should have "hello" and word2 should have "world."

  3. To create a co-process, then use print -p to write to the co-process, and use read -p to read the input from the co-process, type:

    (read; print "hello $REPLY") 
    print -p "world"
    read-p line

    The value of the line variable should have "hello world."

  4. To save a copy of the input line as a command in the history file, type:

    read -s line < input_file

    If input_file contains "echo hello world," then "echo hello world" will be saved as a command in the history file.

Related Information

The printf command.

The ksh command.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]