1read(1) User Commands read(1)
2
3
4
6 read - read a line from standard input
7
9 /usr/bin/read [-r] var...
10
11
12 sh
13 read name...
14
15
16 csh
17 set variable= $<
18
19
20 ksh
21 read [-prsu [n]] [name ? prompt] [name]...
22
23
24 ksh93
25 read [-Aprs] [-d delim] [-n nsize] [-N nsize] [-t timeout][-u unit] [vname?prompt] [vname... ]
26
27
29 /usr/bin/read
30 The read utility reads a single line from standard input.
31
32
33 By default, unless the -r option is specified, backslash (\) acts as an
34 escape character. If standard input is a terminal device and the invok‐
35 ing shell is interactive, read prompts for a continuation line when:
36
37 o The shell reads an input line ending with a backslash,
38 unless the -r option is specified.
39
40 o A here-document is not terminated after a NEWLINE character
41 is entered.
42
43
44 The line is split into fields as in the shell. The first field is
45 assigned to the first variable var, the second field to the second
46 variable var, and so forth. If there are fewer var operands specified
47 than there are fields, the leftover fields and their intervening sepa‐
48 rators is assigned to the last var. If there are fewer fields than
49 vars, the remaining vars is set to empty strings.
50
51
52 The setting of variables specified by the var operands affects the cur‐
53 rent shell execution environment. If it is called in a sub-shell or
54 separate utility execution environment, such as one of the following:
55
56 (read foo)
57 nohup read ...
58 find . -exec read ... \;
59
60
61
62
63 it does not affect the shell variables in the caller's environment.
64
65
66 The standard input must be a text file.
67
68 sh
69 One line is read from the standard input and, using the internal field
70 separator, IFS (normally space or tab), to delimit word boundaries, the
71 first word is assigned to the first name, the second word to the second
72 name, and so on, with leftover words assigned to the last name. Lines
73 can be continued using \newline. Characters other than NEWLINE can be
74 quoted by preceding them with a backslash. These backslashes are
75 removed before words are assigned to names, and no interpretation is
76 done on the character that follows the backslash. The return code is 0,
77 unless an end-of-file is encountered.
78
79 csh
80 The notation:
81
82 set variable = $<
83
84
85
86
87 loads one line of standard input as the value for variable. (See
88 csh(1)).
89
90 ksh
91 The shell input mechanism. One line is read and is broken up into
92 fields using the characters in IFS as separators. The escape character,
93 (\), is used to remove any special meaning for the next character and
94 for line continuation. In raw mode, the -r, the , and the \ character
95 are not treated specially. The first field is assigned to the first
96 name, the second field to the second name, and so on, with leftover
97 fields assigned to the last name. The -p option causes the input line
98 to be taken from the input pipe of a process spawned by the shell using
99 |&. If the -s flag is present, the input is saved as a command in the
100 history file. The flag -u can be used to specify a one digit file
101 descriptor unit n to read from. The file descriptor can be opened with
102 the exec special command. The default value of n is 0. If name is omit‐
103 ted, REPLY is used as the default name. The exit status is 0 unless the
104 input file is not open for reading or an end-of-file is encountered. An
105 end-of-file with the -p option causes cleanup for this process so that
106 another can be spawned. If the first argument contains a ?, the remain‐
107 der of this word is used as a prompt on standard error when the shell
108 is interactive. The exit status is 0 unless an end-of-file is encoun‐
109 tered.
110
111 ksh93
112 read reads a line from standard input and breaks it into fields using
113 the characters in the value of the IFS variable as separators. The
114 escape character, \, is used to remove any special meaning for the next
115 character and for line continuation unless the -r option is specified.
116
117
118 If there are more variables than fields, the remaining variables are
119 set to empty strings. If there are fewer variables than fields, the
120 leftover fields and their intervening separators are assigned to the
121 last variable. If no var is specified, the variable REPLY is used.
122
123
124 When var has the binary attribute and -n or -N is specified, the bytes
125 that are read are stored directly into var.
126
127
128 If you specify ?prompt after the first var, read displays a prompt on
129 standard error when standard input is a terminal or pipe.
130
132 /usr/bin/read, ksh
133 The following option is supported by /usr/bin/read and ksh:
134
135 -r Do not treat a backslash character in any special way. Considers
136 each backslash to be part of the input line.
137
138
139 ksh93
140 The following options are supported by ksh93:
141
142 -A Unset var, and create an indexed array containing each
143 field in the line starting at index 0.
144
145
146 -d delim Read until delimiter delim instead of to the end of line.
147
148
149 -n nsize Read at most nsize bytes. Binary field size is in bytes.
150
151
152 -N nsize Read exactly nsize bytes. Binary field size is in bytes.
153
154
155 -p Read from the current co-process instead of standard
156 input. An end of file causes read to disconnect the co-
157 process so that another can be created.
158
159
160 -r Do not treat \ specially when processing the input line.
161
162
163 -s Save a copy of the input as an entry in the shell history
164 file.
165
166
167 -t timeout Specify a timeout in seconds when reading from a terminal
168 or pipe.
169
170
171 -u fd Read from file descriptor number fd instead of standard
172 input. The default value is 0.
173
174
175 -v When reading from a terminal, display the value of the
176 first variable and use it as a default value.
177
178
180 The following operand is supported:
181
182 var The name of an existing or non-existing shell variable.
183
184
186 Example 1 Using the read Command
187
188
189 The following example for /usr/bin/read prints a file with the first
190 field of each line moved to the end of the line:
191
192
193 example% while read -r xx yy
194 do
195 printf "%s %s\n" "$yy" "$xx"
196 done < input_file
197
198
199
201 See environ(5) for descriptions of the following environment variables
202 that affect the execution of read: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES,
203 and NLSPATH.
204
205 IFS Determines the internal field separators used to delimit fields.
206
207
208 PS2 Provides the prompt string that an interactive shell writes to
209 standard error when a line ending with a backslash is read and
210 the -r option was not specified, or if a here-document is not
211 terminated after a NEWLINE character is entered.
212
213
215 The following exit values are returned:
216
217 0 Successful completion.
218
219
220 >0 End-of-file was detected or an error occurred.
221
222
224 See attributes(5) for descriptions of the following attributes:
225
226 /usr/bin/read, csh, ksh, sh
227 ┌─────────────────────────────┬─────────────────────────────┐
228 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
229 ├─────────────────────────────┼─────────────────────────────┤
230 │Availability │SUNWcsu │
231 ├─────────────────────────────┼─────────────────────────────┤
232 │Interface Stability │Committed │
233 ├─────────────────────────────┼─────────────────────────────┤
234 │Standard │See standards(5). │
235 └─────────────────────────────┴─────────────────────────────┘
236
237 ksh93
238 ┌─────────────────────────────┬─────────────────────────────┐
239 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
240 ├─────────────────────────────┼─────────────────────────────┤
241 │Availability │SUNWcsu │
242 ├─────────────────────────────┼─────────────────────────────┤
243 │Interface Stability │Uncommitted │
244 └─────────────────────────────┴─────────────────────────────┘
245
247 csh(1), ksh(1), ksh93(1), line(1), set(1), sh(1), attributes(5), envi‐
248 ron(5), standards(5)
249
250
251
252SunOS 5.11 18 Dec 2007 read(1)