1SH(1P) POSIX Programmer's Manual SH(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 sh — shell, the standard command language interpreter
14
16 sh [−abCefhimnuvx] [−o option]... [+abCefhimnuvx] [+o option]...
17 [command_file [argument...]]
18
19 sh −c [−abCefhimnuvx] [−o option]... [+abCefhimnuvx] [+o option]...
20 command_string [command_name [argument...]]
21
22 sh −s [−abCefhimnuvx] [−o option]... [+abCefhimnuvx] [+o option]...
23 [argument...]
24
26 The sh utility is a command language interpreter that shall execute
27 commands read from a command line string, the standard input, or a
28 specified file. The application shall ensure that the commands to be
29 executed are expressed in the language described in Chapter 2, Shell
30 Command Language.
31
32 Pathname expansion shall not fail due to the size of a file.
33
34 Shell input and output redirections have an implementation-defined off‐
35 set maximum that is established in the open file description.
36
38 The sh utility shall conform to the Base Definitions volume of
39 POSIX.1‐2008, Section 12.2, Utility Syntax Guidelines, with an exten‐
40 sion for support of a leading <plus-sign> ('+') as noted below.
41
42 The −a, −b, −C, −e, −f, −m, −n, −o option, −u, −v, and −x options are
43 described as part of the set utility in Section 2.14, Special Built-In
44 Utilities. The option letters derived from the set special built-in
45 shall also be accepted with a leading <plus-sign> ('+') instead of a
46 leading <hyphen> (meaning the reverse case of the option as described
47 in this volume of POSIX.1‐2008).
48
49 The following additional options shall be supported:
50
51 −c Read commands from the command_string operand. Set the value
52 of special parameter 0 (see Section 2.5.2, Special Parame‐
53 ters) from the value of the command_name operand and the
54 positional parameters ($1, $2, and so on) in sequence from
55 the remaining argument operands. No commands shall be read
56 from the standard input.
57
58 −i Specify that the shell is interactive; see below. An imple‐
59 mentation may treat specifying the −i option as an error if
60 the real user ID of the calling process does not equal the
61 effective user ID or if the real group ID does not equal the
62 effective group ID.
63
64 −s Read commands from the standard input.
65
66 If there are no operands and the −c option is not specified, the −s
67 option shall be assumed.
68
69 If the −i option is present, or if there are no operands and the
70 shell's standard input and standard error are attached to a terminal,
71 the shell is considered to be interactive.
72
74 The following operands shall be supported:
75
76 − A single <hyphen> shall be treated as the first operand and
77 then ignored. If both '−' and "−−" are given as arguments, or
78 if other operands precede the single <hyphen>, the results
79 are undefined.
80
81 argument The positional parameters ($1, $2, and so on) shall be set to
82 arguments, if any.
83
84 command_file
85 The pathname of a file containing commands. If the pathname
86 contains one or more <slash> characters, the implementation
87 attempts to read that file; the file need not be executable.
88 If the pathname does not contain a <slash> character:
89
90 * The implementation shall attempt to read that file from
91 the current working directory; the file need not be exe‐
92 cutable.
93
94 * If the file is not in the current working directory, the
95 implementation may perform a search for an executable
96 file using the value of PATH, as described in Section
97 2.9.1.1, Command Search and Execution.
98
99 Special parameter 0 (see Section 2.5.2, Special Parameters)
100 shall be set to the value of command_file. If sh is called
101 using a synopsis form that omits command_file, special param‐
102 eter 0 shall be set to the value of the first argument passed
103 to sh from its parent (for example, argv[0] for a C program),
104 which is normally a pathname used to execute the sh utility.
105
106 command_name
107 A string assigned to special parameter 0 when executing the
108 commands in command_string. If command_name is not speci‐
109 fied, special parameter 0 shall be set to the value of the
110 first argument passed to sh from its parent (for example,
111 argv[0] for a C program), which is normally a pathname used
112 to execute the sh utility.
113
114 command_string
115 A string that shall be interpreted by the shell as one or
116 more commands, as if the string were the argument to the sys‐
117 tem() function defined in the System Interfaces volume of
118 POSIX.1‐2008. If the command_string operand is an empty
119 string, sh shall exit with a zero exit status.
120
122 The standard input shall be used only if one of the following is true:
123
124 * The −s option is specified.
125
126 * The −c option is not specified and no operands are specified.
127
128 * The script executes one or more commands that require input from
129 standard input (such as a read command that does not redirect its
130 input).
131
132 See the INPUT FILES section.
133
134 When the shell is using standard input and it invokes a command that
135 also uses standard input, the shell shall ensure that the standard
136 input file pointer points directly after the command it has read when
137 the command begins execution. It shall not read ahead in such a manner
138 that any characters intended to be read by the invoked command are con‐
139 sumed by the shell (whether interpreted by the shell or not) or that
140 characters that are not read by the invoked command are not seen by the
141 shell. When the command expecting to read standard input is started
142 asynchronously by an interactive shell, it is unspecified whether char‐
143 acters are read by the command or interpreted by the shell.
144
145 If the standard input to sh is a FIFO or terminal device and is set to
146 non-blocking reads, then sh shall enable blocking reads on standard
147 input. This shall remain in effect when the command completes.
148
150 The input file shall be a text file, except that line lengths shall be
151 unlimited. If the input file is empty or consists solely of blank lines
152 or comments, or both, sh shall exit with a zero exit status.
153
155 The following environment variables shall affect the execution of sh:
156
157 ENV This variable, when and only when an interactive shell is
158 invoked, shall be subjected to parameter expansion (see Sec‐
159 tion 2.6.2, Parameter Expansion) by the shell, and the
160 resulting value shall be used as a pathname of a file con‐
161 taining shell commands to execute in the current environment.
162 The file need not be executable. If the expanded value of ENV
163 is not an absolute pathname, the results are unspecified.
164 ENV shall be ignored if the real and effective user IDs or
165 real and effective group IDs of the process are different.
166
167 FCEDIT This variable, when expanded by the shell, shall determine
168 the default value for the −e editor option's editor option-
169 argument. If FCEDIT is null or unset, ed shall be used as the
170 editor.
171
172 HISTFILE Determine a pathname naming a command history file. If the
173 HISTFILE variable is not set, the shell may attempt to access
174 or create a file .sh_history in the directory referred to by
175 the HOME environment variable. If the shell cannot obtain
176 both read and write access to, or create, the history file,
177 it shall use an unspecified mechanism that allows the history
178 to operate properly. (References to history ``file'' in this
179 section shall be understood to mean this unspecified mecha‐
180 nism in such cases.) An implementation may choose to access
181 this variable only when initializing the history file; this
182 initialization shall occur when fc or sh first attempt to
183 retrieve entries from, or add entries to, the file, as the
184 result of commands issued by the user, the file named by the
185 ENV variable, or implementation-defined system start-up
186 files. Implementations may choose to disable the history
187 list mechanism for users with appropriate privileges who do
188 not set HISTFILE; the specific circumstances under which this
189 occurs are implementation-defined. If more than one instance
190 of the shell is using the same history file, it is unspeci‐
191 fied how updates to the history file from those shells inter‐
192 act. As entries are deleted from the history file, they shall
193 be deleted oldest first. It is unspecified when history file
194 entries are physically removed from the history file.
195
196 HISTSIZE Determine a decimal number representing the limit to the num‐
197 ber of previous commands that are accessible. If this vari‐
198 able is unset, an unspecified default greater than or equal
199 to 128 shall be used. The maximum number of commands in the
200 history list is unspecified, but shall be at least 128. An
201 implementation may choose to access this variable only when
202 initializing the history file, as described under HISTFILE.
203 Therefore, it is unspecified whether changes made to HISTSIZE
204 after the history file has been initialized are effective.
205
206 HOME Determine the pathname of the user's home directory. The con‐
207 tents of HOME are used in tilde expansion as described in
208 Section 2.6.1, Tilde Expansion.
209
210 IFS A string treated as a list of characters that is used for
211 field splitting and to split lines into fields with the read
212 command.
213
214 If IFS is not set, it shall behave as normal for an unset
215 variable, except that field splitting by the shell and line
216 splitting by the read command shall be performed as if the
217 value of IFS is <space><tab><newline>; see Section 2.6.5,
218 Field Splitting.
219
220 Implementations may ignore the value of IFS in the environ‐
221 ment, or the absence of IFS from the environment, at the time
222 the shell is invoked, in which case the shell shall set IFS
223 to <space><tab><newline> when it is invoked.
224
225 LANG Provide a default value for the internationalization vari‐
226 ables that are unset or null. (See the Base Definitions vol‐
227 ume of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
228 ables for the precedence of internationalization variables
229 used to determine the values of locale categories.)
230
231 LC_ALL If set to a non-empty string value, override the values of
232 all the other internationalization variables.
233
234 LC_COLLATE
235 Determine the behavior of range expressions, equivalence
236 classes, and multi-character collating elements within pat‐
237 tern matching.
238
239 LC_CTYPE Determine the locale for the interpretation of sequences of
240 bytes of text data as characters (for example, single-byte as
241 opposed to multi-byte characters in arguments and input
242 files), which characters are defined as letters (character
243 class alpha), and the behavior of character classes within
244 pattern matching.
245
246 LC_MESSAGES
247 Determine the locale that should be used to affect the format
248 and contents of diagnostic messages written to standard
249 error.
250
251 MAIL Determine a pathname of the user's mailbox file for purposes
252 of incoming mail notification. If this variable is set, the
253 shell shall inform the user if the file named by the variable
254 is created or if its modification time has changed. Informing
255 the user shall be accomplished by writing a string of unspec‐
256 ified format to standard error prior to the writing of the
257 next primary prompt string. Such check shall be performed
258 only after the completion of the interval defined by the
259 MAILCHECK variable after the last such check. The user shall
260 be informed only if MAIL is set and MAILPATH is not set.
261
262 MAILCHECK
263 Establish a decimal integer value that specifies how often
264 (in seconds) the shell shall check for the arrival of mail in
265 the files specified by the MAILPATH or MAIL variables. The
266 default value shall be 600 seconds. If set to zero, the shell
267 shall check before issuing each primary prompt.
268
269 MAILPATH Provide a list of pathnames and optional messages separated
270 by <colon> characters. If this variable is set, the shell
271 shall inform the user if any of the files named by the vari‐
272 able are created or if any of their modification times
273 change. (See the preceding entry for MAIL for descriptions of
274 mail arrival and user informing.) Each pathname can be fol‐
275 lowed by '%' and a string that shall be subjected to parame‐
276 ter expansion and written to standard error when the modifi‐
277 cation time changes. If a '%' character in the pathname is
278 preceded by a <backslash>, it shall be treated as a literal
279 '%' in the pathname. The default message is unspecified.
280
281 The MAILPATH environment variable takes precedence over the
282 MAIL variable.
283
284 NLSPATH Determine the location of message catalogs for the processing
285 of LC_MESSAGES.
286
287 PATH Establish a string formatted as described in the Base Defini‐
288 tions volume of POSIX.1‐2008, Chapter 8, Environment Vari‐
289 ables, used to effect command interpretation; see Section
290 2.9.1.1, Command Search and Execution.
291
292 PWD This variable shall represent an absolute pathname of the
293 current working directory. Assignments to this variable may
294 be ignored.
295
297 The sh utility shall take the standard action for all signals (see Sec‐
298 tion 1.4, Utility Description Defaults) with the following exceptions.
299
300 If the shell is interactive, SIGINT signals received during command
301 line editing shall be handled as described in the EXTENDED DESCRIPTION,
302 and SIGINT signals received at other times shall be caught but no
303 action performed.
304
305 If the shell is interactive:
306
307 * SIGQUIT and SIGTERM signals shall be ignored.
308
309 * If the −m option is in effect, SIGTTIN, SIGTTOU, and SIGTSTP sig‐
310 nals shall be ignored.
311
312 * If the −m option is not in effect, it is unspecified whether SIGT‐
313 TIN, SIGTTOU, and SIGTSTP signals are ignored, set to the default
314 action, or caught. If they are caught, the shell shall, in the
315 signal-catching function, set the signal to the default action and
316 raise the signal (after taking any appropriate steps, such as
317 restoring terminal settings).
318
319 The standard actions, and the actions described above for interactive
320 shells, can be overridden by use of the trap special built-in utility
321 (see trap and Section 2.11, Signals and Error Handling).
322
324 See the STDERR section.
325
327 Except as otherwise stated (by the descriptions of any invoked utili‐
328 ties or in interactive mode), standard error shall be used only for
329 diagnostic messages.
330
332 None.
333
335 See Chapter 2, Shell Command Language. The functionality described in
336 the rest of the EXTENDED DESCRIPTION section shall be provided on
337 implementations that support the User Portability Utilities option (and
338 the rest of this section is not further shaded for this option).
339
340 Command History List
341 When the sh utility is being used interactively, it shall maintain a
342 list of commands previously entered from the terminal in the file named
343 by the HISTFILE environment variable. The type, size, and internal for‐
344 mat of this file are unspecified. Multiple sh processes can share
345 access to the file for a user, if file access permissions allow this;
346 see the description of the HISTFILE environment variable.
347
348 Command Line Editing
349 When sh is being used interactively from a terminal, the current com‐
350 mand and the command history (see fc) can be edited using vi-mode com‐
351 mand line editing. This mode uses commands, described below, similar to
352 a subset of those described in the vi utility. Implementations may
353 offer other command line editing modes corresponding to other editing
354 utilities.
355
356 The command set −o vi shall enable vi-mode editing and place sh into vi
357 insert mode (see Command Line Editing (vi-mode)). This command also
358 shall disable any other editing mode that the implementation may pro‐
359 vide. The command set +o vi disables vi-mode editing.
360
361 Certain block-mode terminals may be unable to support shell command
362 line editing. If a terminal is unable to provide either edit mode, it
363 need not be possible to set −o vi when using the shell on this termi‐
364 nal.
365
366 In the following sections, the characters erase, interrupt, kill, and
367 end-of-file are those set by the stty utility.
368
369 Command Line Editing (vi-mode)
370 In vi editing mode, there shall be a distinguished line, the edit line.
371 All the editing operations which modify a line affect the edit line.
372 The edit line is always the newest line in the command history buffer.
373
374 With vi-mode enabled, sh can be switched between insert mode and com‐
375 mand mode.
376
377 When in insert mode, an entered character shall be inserted into the
378 command line, except as noted in vi Line Editing Insert Mode. Upon
379 entering sh and after termination of the previous command, sh shall be
380 in insert mode.
381
382 Typing an escape character shall switch sh into command mode (see vi
383 Line Editing Command Mode). In command mode, an entered character
384 shall either invoke a defined operation, be used as part of a multi-
385 character operation, or be treated as an error. A character that is not
386 recognized as part of an editing command shall terminate any specific
387 editing command and shall alert the terminal. If sh receives a SIGINT
388 signal in command mode (whether generated by typing the interrupt char‐
389 acter or by other means), it shall terminate command line editing on
390 the current command line, reissue the prompt on the next line of the
391 terminal, and reset the command history (see fc) so that the most
392 recently executed command is the previous command (that is, the command
393 that was being edited when it was interrupted is not re-entered into
394 the history).
395
396 In the following sections, the phrase ``move the cursor to the begin‐
397 ning of the word'' shall mean ``move the cursor to the first character
398 of the current word'' and the phrase ``move the cursor to the end of
399 the word'' shall mean ``move the cursor to the last character of the
400 current word''. The phrase ``beginning of the command line'' indicates
401 the point between the end of the prompt string issued by the shell (or
402 the beginning of the terminal line, if there is no prompt string) and
403 the first character of the command text.
404
405 vi Line Editing Insert Mode
406 While in insert mode, any character typed shall be inserted in the cur‐
407 rent command line, unless it is from the following set.
408
409 <newline> Execute the current command line. If the current command line
410 is not empty, this line shall be entered into the command
411 history (see fc).
412
413 erase Delete the character previous to the current cursor position
414 and move the current cursor position back one character. In
415 insert mode, characters shall be erased from both the screen
416 and the buffer when backspacing.
417
418 interrupt If sh receives a SIGINT signal in insert mode (whether gener‐
419 ated by typing the interrupt character or by other means), it
420 shall terminate command line editing with the same effects as
421 described for interrupting command mode; see Command Line
422 Editing (vi-mode).
423
424 kill Clear all the characters from the input line.
425
426 <control>‐V
427 Insert the next character input, even if the character is
428 otherwise a special insert mode character.
429
430 <control>‐W
431 Delete the characters from the one preceding the cursor to
432 the preceding word boundary. The word boundary in this case
433 is the closer to the cursor of either the beginning of the
434 line or a character that is in neither the blank nor punct
435 character classification of the current locale.
436
437 end-of-file
438 Interpreted as the end of input in sh. This interpretation
439 shall occur only at the beginning of an input line. If end-
440 of-file is entered other than at the beginning of the line,
441 the results are unspecified.
442
443 <ESC> Place sh into command mode.
444
445 vi Line Editing Command Mode
446 In command mode for the command line editing feature, decimal digits
447 not beginning with 0 that precede a command letter shall be remembered.
448 Some commands use these decimal digits as a count number that affects
449 the operation.
450
451 The term motion command represents one of the commands:
452
453 <space> 0 b F l W ^ $ ; E f T w | , B e h t
454
455 If the current line is not the edit line, any command that modifies the
456 current line shall cause the content of the current line to replace the
457 content of the edit line, and the current line shall become the edit
458 line. This replacement cannot be undone (see the u and U commands
459 below). The modification requested shall then be performed to the edit
460 line. When the current line is the edit line, the modification shall be
461 done directly to the edit line.
462
463 Any command that is preceded by count shall take a count (the numeric
464 value of any preceding decimal digits). Unless otherwise noted, this
465 count shall cause the specified operation to repeat by the number of
466 times specified by the count. Also unless otherwise noted, a count
467 that is out of range is considered an error condition and shall alert
468 the terminal, but neither the cursor position, nor the command line,
469 shall change.
470
471 The terms word and bigword are used as defined in the vi description.
472 The term save buffer corresponds to the term unnamed buffer in vi.
473
474 The following commands shall be recognized in command mode:
475
476 <newline> Execute the current command line. If the current command line
477 is not empty, this line shall be entered into the command
478 history (see fc).
479
480 <control>‐L
481 Redraw the current command line. Position the cursor at the
482 same location on the redrawn line.
483
484 # Insert the character '#' at the beginning of the current com‐
485 mand line and treat the resulting edit line as a comment.
486 This line shall be entered into the command history; see fc.
487
488 = Display the possible shell word expansions (see Section 2.6,
489 Word Expansions) of the bigword at the current command line
490 position.
491
492 Note: This does not modify the content of the current
493 line, and therefore does not cause the current line
494 to become the edit line.
495
496 These expansions shall be displayed on subsequent terminal
497 lines. If the bigword contains none of the characters '?',
498 '*', or '[', an <asterisk> ('*') shall be implicitly assumed
499 at the end. If any directories are matched, these expansions
500 shall have a '/' character appended. After the expansion, the
501 line shall be redrawn, the cursor repositioned at the current
502 cursor position, and sh shall be placed in command mode.
503
504 \ Perform pathname expansion (see Section 2.6.6, Pathname
505 Expansion) on the current bigword, up to the largest set of
506 characters that can be matched uniquely. If the bigword con‐
507 tains none of the characters '?', '*', or '[', an <asterisk>
508 ('*') shall be implicitly assumed at the end. This maximal
509 expansion then shall replace the original bigword in the com‐
510 mand line, and the cursor shall be placed after this expan‐
511 sion. If the resulting bigword completely and uniquely
512 matches a directory, a '/' character shall be inserted
513 directly after the bigword. If some other file is completely
514 matched, a single <space> shall be inserted after the big‐
515 word. After this operation, sh shall be placed in insert
516 mode.
517
518 * Perform pathname expansion on the current bigword and insert
519 all expansions into the command to replace the current big‐
520 word, with each expansion separated by a single <space>. If
521 at the end of the line, the current cursor position shall be
522 moved to the first column position following the expansions
523 and sh shall be placed in insert mode. Otherwise, the current
524 cursor position shall be the last column position of the
525 first character after the expansions and sh shall be placed
526 in insert mode. If the current bigword contains none of the
527 characters '?', '*', or '[', before the operation, an <aster‐
528 isk> ('*') shall be implicitly assumed at the end.
529
530 @letter Insert the value of the alias named _letter. The symbol let‐
531 ter represents a single alphabetic character from the porta‐
532 ble character set; implementations may support additional
533 characters as an extension. If the alias _letter contains
534 other editing commands, these commands shall be performed as
535 part of the insertion. If no alias _letter is enabled, this
536 command shall have no effect.
537
538 [count]~ Convert, if the current character is a lowercase letter, to
539 the equivalent uppercase letter and vice versa, as prescribed
540 by the current locale. The current cursor position then shall
541 be advanced by one character. If the cursor was positioned on
542 the last character of the line, the case conversion shall
543 occur, but the cursor shall not advance. If the '~' command
544 is preceded by a count, that number of characters shall be
545 converted, and the cursor shall be advanced to the character
546 position after the last character converted. If the count is
547 larger than the number of characters after the cursor, this
548 shall not be considered an error; the cursor shall advance to
549 the last character on the line.
550
551 [count]. Repeat the most recent non-motion command, even if it was
552 executed on an earlier command line. If the previous command
553 was preceded by a count, and no count is given on the '.'
554 command, the count from the previous command shall be
555 included as part of the repeated command. If the '.' command
556 is preceded by a count, this shall override any count argu‐
557 ment to the previous command. The count specified in the '.'
558 command shall become the count for subsequent '.' commands
559 issued without a count.
560
561 [number]v Invoke the vi editor to edit the current command line in a
562 temporary file. When the editor exits, the commands in the
563 temporary file shall be executed and placed in the command
564 history. If a number is included, it specifies the command
565 number in the command history to be edited, rather than the
566 current command line.
567
568 [count]l (ell)
569
570 [count]<space>
571 Move the current cursor position to the next character posi‐
572 tion. If the cursor was positioned on the last character of
573 the line, the terminal shall be alerted and the cursor shall
574 not be advanced. If the count is larger than the number of
575 characters after the cursor, this shall not be considered an
576 error; the cursor shall advance to the last character on the
577 line.
578
579 [count]h Move the current cursor position to the countth (default 1)
580 previous character position. If the cursor was positioned on
581 the first character of the line, the terminal shall be
582 alerted and the cursor shall not be moved. If the count is
583 larger than the number of characters before the cursor, this
584 shall not be considered an error; the cursor shall move to
585 the first character on the line.
586
587 [count]w Move to the start of the next word. If the cursor was posi‐
588 tioned on the last character of the line, the terminal shall
589 be alerted and the cursor shall not be advanced. If the count
590 is larger than the number of words after the cursor, this
591 shall not be considered an error; the cursor shall advance to
592 the last character on the line.
593
594 [count]W Move to the start of the next bigword. If the cursor was
595 positioned on the last character of the line, the terminal
596 shall be alerted and the cursor shall not be advanced. If the
597 count is larger than the number of bigwords after the cursor,
598 this shall not be considered an error; the cursor shall
599 advance to the last character on the line.
600
601 [count]e Move to the end of the current word. If at the end of a word,
602 move to the end of the next word. If the cursor was posi‐
603 tioned on the last character of the line, the terminal shall
604 be alerted and the cursor shall not be advanced. If the count
605 is larger than the number of words after the cursor, this
606 shall not be considered an error; the cursor shall advance to
607 the last character on the line.
608
609 [count]E Move to the end of the current bigword. If at the end of a
610 bigword, move to the end of the next bigword. If the cursor
611 was positioned on the last character of the line, the termi‐
612 nal shall be alerted and the cursor shall not be advanced. If
613 the count is larger than the number of bigwords after the
614 cursor, this shall not be considered an error; the cursor
615 shall advance to the last character on the line.
616
617 [count]b Move to the beginning of the current word. If at the begin‐
618 ning of a word, move to the beginning of the previous word.
619 If the cursor was positioned on the first character of the
620 line, the terminal shall be alerted and the cursor shall not
621 be moved. If the count is larger than the number of words
622 preceding the cursor, this shall not be considered an error;
623 the cursor shall return to the first character on the line.
624
625 [count]B Move to the beginning of the current bigword. If at the
626 beginning of a bigword, move to the beginning of the previous
627 bigword. If the cursor was positioned on the first character
628 of the line, the terminal shall be alerted and the cursor
629 shall not be moved. If the count is larger than the number of
630 bigwords preceding the cursor, this shall not be considered
631 an error; the cursor shall return to the first character on
632 the line.
633
634 ^ Move the current cursor position to the first character on
635 the input line that is not a <blank>.
636
637 $ Move to the last character position on the current command
638 line.
639
640 0 (Zero.) Move to the first character position on the current
641 command line.
642
643 [count]| Move to the countth character position on the current command
644 line. If no number is specified, move to the first position.
645 The first character position shall be numbered 1. If the
646 count is larger than the number of characters on the line,
647 this shall not be considered an error; the cursor shall be
648 placed on the last character on the line.
649
650 [count]fc Move to the first occurrence of the character 'c' that occurs
651 after the current cursor position. If the cursor was posi‐
652 tioned on the last character of the line, the terminal shall
653 be alerted and the cursor shall not be advanced. If the char‐
654 acter 'c' does not occur in the line after the current cursor
655 position, the terminal shall be alerted and the cursor shall
656 not be moved.
657
658 [count]Fc Move to the first occurrence of the character 'c' that occurs
659 before the current cursor position. If the cursor was posi‐
660 tioned on the first character of the line, the terminal shall
661 be alerted and the cursor shall not be moved. If the charac‐
662 ter 'c' does not occur in the line before the current cursor
663 position, the terminal shall be alerted and the cursor shall
664 not be moved.
665
666 [count]tc Move to the character before the first occurrence of the
667 character 'c' that occurs after the current cursor position.
668 If the cursor was positioned on the last character of the
669 line, the terminal shall be alerted and the cursor shall not
670 be advanced. If the character 'c' does not occur in the line
671 after the current cursor position, the terminal shall be
672 alerted and the cursor shall not be moved.
673
674 [count]Tc Move to the character after the first occurrence of the char‐
675 acter 'c' that occurs before the current cursor position. If
676 the cursor was positioned on the first character of the line,
677 the terminal shall be alerted and the cursor shall not be
678 moved. If the character 'c' does not occur in the line before
679 the current cursor position, the terminal shall be alerted
680 and the cursor shall not be moved.
681
682 [count]; Repeat the most recent f, F, t, or T command. Any number
683 argument on that previous command shall be ignored. Errors
684 are those described for the repeated command.
685
686 [count], Repeat the most recent f, F, t, or T command. Any number
687 argument on that previous command shall be ignored. However,
688 reverse the direction of that command.
689
690 a Enter insert mode after the current cursor position. Charac‐
691 ters that are entered shall be inserted before the next char‐
692 acter.
693
694 A Enter insert mode after the end of the current command line.
695
696 i Enter insert mode at the current cursor position. Characters
697 that are entered shall be inserted before the current charac‐
698 ter.
699
700 I Enter insert mode at the beginning of the current command
701 line.
702
703 R Enter insert mode, replacing characters from the command line
704 beginning at the current cursor position.
705
706 [count]cmotion
707 Delete the characters between the current cursor position and
708 the cursor position that would result from the specified
709 motion command. Then enter insert mode before the first char‐
710 acter following any deleted characters. If count is speci‐
711 fied, it shall be applied to the motion command. A count
712 shall be ignored for the following motion commands:
713
714 0 ^ $ c
715
716 If the motion command is the character 'c', the current com‐
717 mand line shall be cleared and insert mode shall be entered.
718 If the motion command would move the current cursor position
719 toward the beginning of the command line, the character under
720 the current cursor position shall not be deleted. If the
721 motion command would move the current cursor position toward
722 the end of the command line, the character under the current
723 cursor position shall be deleted. If the count is larger
724 than the number of characters between the current cursor
725 position and the end of the command line toward which the
726 motion command would move the cursor, this shall not be con‐
727 sidered an error; all of the remaining characters in the
728 aforementioned range shall be deleted and insert mode shall
729 be entered. If the motion command is invalid, the terminal
730 shall be alerted, the cursor shall not be moved, and no text
731 shall be deleted.
732
733 C Delete from the current character to the end of the line and
734 enter insert mode at the new end-of-line.
735
736 S Clear the entire edit line and enter insert mode.
737
738 [count]rc Replace the current character with the character 'c'. With a
739 number count, replace the current and the following count−1
740 characters. After this command, the current cursor position
741 shall be on the last character that was changed. If the count
742 is larger than the number of characters after the cursor,
743 this shall not be considered an error; all of the remaining
744 characters shall be changed.
745
746 [count]_ Append a <space> after the current character position and
747 then append the last bigword in the previous input line after
748 the <space>. Then enter insert mode after the last character
749 just appended. With a number count, append the countth big‐
750 word in the previous line.
751
752 [count]x Delete the character at the current cursor position and place
753 the deleted characters in the save buffer. If the cursor was
754 positioned on the last character of the line, the character
755 shall be deleted and the cursor position shall be moved to
756 the previous character (the new last character). If the count
757 is larger than the number of characters after the cursor,
758 this shall not be considered an error; all the characters
759 from the cursor to the end of the line shall be deleted.
760
761 [count]X Delete the character before the current cursor position and
762 place the deleted characters in the save buffer. The charac‐
763 ter under the current cursor position shall not change. If
764 the cursor was positioned on the first character of the line,
765 the terminal shall be alerted, and the X command shall have
766 no effect. If the line contained a single character, the X
767 command shall have no effect. If the line contained no char‐
768 acters, the terminal shall be alerted and the cursor shall
769 not be moved. If the count is larger than the number of char‐
770 acters before the cursor, this shall not be considered an
771 error; all the characters from before the cursor to the
772 beginning of the line shall be deleted.
773
774 [count]dmotion
775 Delete the characters between the current cursor position and
776 the character position that would result from the motion com‐
777 mand. A number count repeats the motion command count times.
778 If the motion command would move toward the beginning of the
779 command line, the character under the current cursor position
780 shall not be deleted. If the motion command is d, the entire
781 current command line shall be cleared. If the count is larger
782 than the number of characters between the current cursor
783 position and the end of the command line toward which the
784 motion command would move the cursor, this shall not be con‐
785 sidered an error; all of the remaining characters in the
786 aforementioned range shall be deleted. The deleted characters
787 shall be placed in the save buffer.
788
789 D Delete all characters from the current cursor position to the
790 end of the line. The deleted characters shall be placed in
791 the save buffer.
792
793 [count]ymotion
794 Yank (that is, copy) the characters from the current cursor
795 position to the position resulting from the motion command
796 into the save buffer. A number count shall be applied to the
797 motion command. If the motion command would move toward the
798 beginning of the command line, the character under the cur‐
799 rent cursor position shall not be included in the set of
800 yanked characters. If the motion command is y, the entire
801 current command line shall be yanked into the save buffer.
802 The current cursor position shall be unchanged. If the count
803 is larger than the number of characters between the current
804 cursor position and the end of the command line toward which
805 the motion command would move the cursor, this shall not be
806 considered an error; all of the remaining characters in the
807 aforementioned range shall be yanked.
808
809 Y Yank the characters from the current cursor position to the
810 end of the line into the save buffer. The current character
811 position shall be unchanged.
812
813 [count]p Put a copy of the current contents of the save buffer after
814 the current cursor position. The current cursor position
815 shall be advanced to the last character put from the save
816 buffer. A count shall indicate how many copies of the save
817 buffer shall be put.
818
819 [count]P Put a copy of the current contents of the save buffer before
820 the current cursor position. The current cursor position
821 shall be moved to the last character put from the save buf‐
822 fer. A count shall indicate how many copies of the save buf‐
823 fer shall be put.
824
825 u Undo the last command that changed the edit line. This opera‐
826 tion shall not undo the copy of any command line to the edit
827 line.
828
829 U Undo all changes made to the edit line. This operation shall
830 not undo the copy of any command line to the edit line.
831
832 [count]k
833
834 [count]− Set the current command line to be the countth previous com‐
835 mand line in the shell command history. If count is not spec‐
836 ified, it shall default to 1. The cursor shall be positioned
837 on the first character of the new command. If a k or − com‐
838 mand would retreat past the maximum number of commands in
839 effect for this shell (affected by the HISTSIZE environment
840 variable), the terminal shall be alerted, and the command
841 shall have no effect.
842
843 [count]j
844
845 [count]+ Set the current command line to be the countth next command
846 line in the shell command history. If count is not specified,
847 it shall default to 1. The cursor shall be positioned on the
848 first character of the new command. If a j or + command
849 advances past the edit line, the current command line shall
850 be restored to the edit line and the terminal shall be
851 alerted.
852
853 [number]G Set the current command line to be the oldest command line
854 stored in the shell command history. With a number number,
855 set the current command line to be the command line number in
856 the history. If command line number does not exist, the ter‐
857 minal shall be alerted and the command line shall not be
858 changed.
859
860 /pattern<newline>
861 Move backwards through the command history, searching for the
862 specified pattern, beginning with the previous command line.
863 Patterns use the pattern matching notation described in Sec‐
864 tion 2.13, Pattern Matching Notation, except that the '^'
865 character shall have special meaning when it appears as the
866 first character of pattern. In this case, the '^' is dis‐
867 carded and the characters after the '^' shall be matched only
868 at the beginning of a line. Commands in the command history
869 shall be treated as strings, not as filenames. If the pattern
870 is not found, the current command line shall be unchanged and
871 the terminal is alerted. If it is found in a previous line,
872 the current command line shall be set to that line and the
873 cursor shall be set to the first character of the new command
874 line.
875
876 If pattern is empty, the last non-empty pattern provided to /
877 or ? shall be used. If there is no previous non-empty pat‐
878 tern, the terminal shall be alerted and the current command
879 line shall remain unchanged.
880
881 ?pattern<newline>
882 Move forwards through the command history, searching for the
883 specified pattern, beginning with the next command line. Pat‐
884 terns use the pattern matching notation described in Section
885 2.13, Pattern Matching Notation, except that the '^' charac‐
886 ter shall have special meaning when it appears as the first
887 character of pattern. In this case, the '^' is discarded and
888 the characters after the '^' shall be matched only at the
889 beginning of a line. Commands in the command history shall be
890 treated as strings, not as filenames. If the pattern is not
891 found, the current command line shall be unchanged and the
892 terminal alerted. If it is found in a following line, the
893 current command line shall be set to that line and the cursor
894 shall be set to the fist character of the new command line.
895
896 If pattern is empty, the last non-empty pattern provided to /
897 or ? shall be used. If there is no previous non-empty pat‐
898 tern, the terminal shall be alerted and the current command
899 line shall remain unchanged.
900
901 n Repeat the most recent / or ? command. If there is no previ‐
902 ous / or ?, the terminal shall be alerted and the current
903 command line shall remain unchanged.
904
905 N Repeat the most recent / or ? command, reversing the direc‐
906 tion of the search. If there is no previous / or ?, the ter‐
907 minal shall be alerted and the current command line shall
908 remain unchanged.
909
911 The following exit values shall be returned:
912
913 0 The script to be executed consisted solely of zero or more
914 blank lines or comments, or both.
915
916 1‐125 A non-interactive shell detected an error other than com‐
917 mand_file not found, including but not limited to syntax, redi‐
918 rection, or variable assignment errors.
919
920 127 A specified command_file could not be found by a non-interac‐
921 tive shell.
922
923 Otherwise, the shell shall return the exit status of the last command
924 it invoked or attempted to invoke (see also the exit utility in Section
925 2.14, Special Built-In Utilities).
926
928 See Section 2.8.1, Consequences of Shell Errors.
929
930 The following sections are informative.
931
933 Standard input and standard error are the files that determine whether
934 a shell is interactive when −i is not specified. For example:
935
936 sh > file
937
938 and:
939
940 sh 2> file
941
942 create interactive and non-interactive shells, respectively. Although
943 both accept terminal input, the results of error conditions are differ‐
944 ent, as described in Section 2.8.1, Consequences of Shell Errors; in
945 the second example a redirection error encountered by a special built-
946 in utility aborts the shell.
947
948 A conforming application must protect its first operand, if it starts
949 with a <plus-sign>, by preceding it with the "−−" argument that denotes
950 the end of the options.
951
952 Applications should note that the standard PATH to the shell cannot be
953 assumed to be either /bin/sh or /usr/bin/sh, and should be determined
954 by interrogation of the PATH returned by getconf PATH, ensuring that
955 the returned pathname is an absolute pathname and not a shell built-in.
956
957 For example, to determine the location of the standard sh utility:
958
959 command −v sh
960
961 On some implementations this might return:
962
963 /usr/xpg4/bin/sh
964
965 Furthermore, on systems that support executable scripts (the "#!" con‐
966 struct), it is recommended that applications using executable scripts
967 install them using getconf PATH to determine the shell pathname and
968 update the "#!" script appropriately as it is being installed (for
969 example, with sed). For example:
970
971 #
972 # Installation time script to install correct POSIX shell pathname
973 #
974 # Get list of paths to check
975 #
976 Sifs=$IFS
977 Sifs_set=${IFS+y}
978 IFS=:
979 set −− $(getconf PATH)
980 if [ "$Sifs_set" = y ]
981 then
982 IFS=$Sifs
983 else
984 unset IFS
985 fi
986 #
987 # Check each path for 'sh'
988 #
989 for i
990 do
991 if [ −x "${i}"/sh ]
992 then
993 Pshell=${i}/sh
994 fi
995 done
996 #
997 # This is the list of scripts to update. They should be of the
998 # form '${name}.source' and will be transformed to '${name}'.
999 # Each script should begin:
1000 #
1001 # #!INSTALLSHELLPATH
1002 #
1003 scripts="a b c"
1004 #
1005 # Transform each script
1006 #
1007 for i in ${scripts}
1008 do
1009 sed −e "s|INSTALLSHELLPATH|${Pshell}|" < ${i}.source > ${i}
1010 done
1011
1013 1. Execute a shell command from a string:
1014
1015 sh −c "cat myfile"
1016
1017 2. Execute a shell script from a file in the current directory:
1018
1019 sh my_shell_cmds
1020
1022 The sh utility and the set special built-in utility share a common set
1023 of options.
1024
1025 The name IFS was originally an abbreviation of ``Input Field Separa‐
1026 tors''; however, this name is misleading as the IFS characters are
1027 actually used as field terminators. The KornShell ignores the contents
1028 of IFS upon entry to the script. A conforming application cannot rely
1029 on importing IFS. One justification for this, beyond security consid‐
1030 erations, is to assist possible future shell compilers. Allowing IFS to
1031 be imported from the environment prevents many optimizations that might
1032 otherwise be performed via dataflow analysis of the script itself.
1033
1034 The text in the STDIN section about non-blocking reads concerns an
1035 instance of sh that has been invoked, probably by a C-language program,
1036 with standard input that has been opened using the O_NONBLOCK flag; see
1037 open() in the System Interfaces volume of POSIX.1‐2008. If the shell
1038 did not reset this flag, it would immediately terminate because no
1039 input data would be available yet and that would be considered the same
1040 as end-of-file.
1041
1042 The options associated with a restricted shell (command name rsh and
1043 the −r option) were excluded because the standard developers considered
1044 that the implied level of security could not be achieved and they did
1045 not want to raise false expectations.
1046
1047 On systems that support set-user-ID scripts, a historical trapdoor has
1048 been to link a script to the name −i. When it is called by a sequence
1049 such as:
1050
1051 sh −
1052
1053 or by:
1054
1055 #! usr/bin/sh −
1056
1057 the historical systems have assumed that no option letters follow.
1058 Thus, this volume of POSIX.1‐2008 allows the single <hyphen> to mark
1059 the end of the options, in addition to the use of the regular "−−"
1060 argument, because it was considered that the older practice was so per‐
1061 vasive. An alternative approach is taken by the KornShell, where real
1062 and effective user/group IDs must match for an interactive shell; this
1063 behavior is specifically allowed by this volume of POSIX.1‐2008.
1064
1065 Note: There are other problems with set-user-ID scripts that the
1066 two approaches described here do not resolve.
1067
1068 The initialization process for the history file can be dependent on the
1069 system start-up files, in that they may contain commands that effec‐
1070 tively preempt the user's settings of HISTFILE and HISTSIZE. For exam‐
1071 ple, function definition commands are recorded in the history file,
1072 unless the set −o nolog option is set. If the system administrator
1073 includes function definitions in some system start-up file called
1074 before the ENV file, the history file is initialized before the user
1075 gets a chance to influence its characteristics. In some historical
1076 shells, the history file is initialized just after the ENV file has
1077 been processed. Therefore, it is implementation-defined whether changes
1078 made to HISTFILE after the history file has been initialized are effec‐
1079 tive.
1080
1081 The default messages for the various MAIL-related messages are unspeci‐
1082 fied because they vary across implementations. Typical messages are:
1083
1084 "you have mail\n"
1085
1086 or:
1087
1088 "you have new mail\n"
1089
1090 It is important that the descriptions of command line editing refer to
1091 the same shell as that in POSIX.1‐2008 so that interactive users can
1092 also be application programmers without having to deal with program‐
1093 matic differences in their two environments. It is also essential that
1094 the utility name sh be specified because this explicit utility name is
1095 too firmly rooted in historical practice of application programs for it
1096 to change.
1097
1098 Consideration was given to mandating a diagnostic message when attempt‐
1099 ing to set vi-mode on terminals that do not support command line edit‐
1100 ing. However, it is not historical practice for the shell to be cog‐
1101 nizant of all terminal types and thus be able to detect inappropriate
1102 terminals in all cases. Implementations are encouraged to supply diag‐
1103 nostics in this case whenever possible, rather than leaving the user in
1104 a state where editing commands work incorrectly.
1105
1106 In early proposals, the KornShell-derived emacs mode of command line
1107 editing was included, even though the emacs editor itself was not. The
1108 community of emacs proponents was adamant that the full emacs editor
1109 not be standardized because they were concerned that an attempt to
1110 standardize this very powerful environment would encourage vendors to
1111 ship strictly conforming versions lacking the extensibility required by
1112 the community. The author of the original emacs program also expressed
1113 his desire to omit the program. Furthermore, there were a number of
1114 historical systems that did not include emacs, or included it without
1115 supporting it, but there were very few that did not include and support
1116 vi. The shell emacs command line editing mode was finally omitted
1117 because it became apparent that the KornShell version and the editor
1118 being distributed with the GNU system had diverged in some respects.
1119 The author of emacs requested that the POSIX emacs mode either be
1120 deleted or have a significant number of unspecified conditions.
1121 Although the KornShell author agreed to consider changes to bring the
1122 shell into alignment, the standard developers decided to defer specifi‐
1123 cation at that time. At the time, it was assumed that convergence on an
1124 acceptable definition would occur for a subsequent draft, but that has
1125 not happened, and there appears to be no impetus to do so. In any case,
1126 implementations are free to offer additional command line editing modes
1127 based on the exact models of editors their users are most comfortable
1128 with.
1129
1130 Early proposals had the following list entry in vi Line Editing Insert
1131 Mode:
1132
1133 \ If followed by the erase or kill character, that character shall
1134 be inserted into the input line. Otherwise, the <backslash>
1135 itself shall be inserted into the input line.
1136
1137 However, this is not actually a feature of sh command line editing
1138 insert mode, but one of some historical terminal line drivers. Some
1139 conforming implementations continue to do this when the stty iexten
1140 flag is set.
1141
1142 In interactive shells, SIGTERM is ignored so that kill 0 does not kill
1143 the shell, and SIGINT is caught so that wait is interruptible. If the
1144 shell does not ignore SIGTTIN, SIGTTOU, and SIGTSTP signals when it is
1145 interactive and the −m option is not in effect, these signals suspend
1146 the shell if it is not a session leader. If it is a session leader, the
1147 signals are discarded if they would stop the process, as required by
1148 the System Interfaces volume of POSIX.1‐2008, Section 2.4.3, Signal
1149 Actions for orphaned process groups.
1150
1152 None.
1153
1155 Chapter 2, Shell Command Language, cd, echo, exit, fc, pwd, invalid,
1156 set, stty, test, trap, umask, vi
1157
1158 The Base Definitions volume of POSIX.1‐2008, Chapter 8, Environment
1159 Variables, Section 12.2, Utility Syntax Guidelines
1160
1161 The System Interfaces volume of POSIX.1‐2008, dup(), exec, exit(),
1162 fork(), open(), pipe(), signal(), system(), ulimit(), umask(), wait()
1163
1165 Portions of this text are reprinted and reproduced in electronic form
1166 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
1167 -- Portable Operating System Interface (POSIX), The Open Group Base
1168 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
1169 cal and Electronics Engineers, Inc and The Open Group. (This is
1170 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
1171 event of any discrepancy between this version and the original IEEE and
1172 The Open Group Standard, the original IEEE and The Open Group Standard
1173 is the referee document. The original Standard can be obtained online
1174 at http://www.unix.org/online.html .
1175
1176 Any typographical or formatting errors that appear in this page are
1177 most likely to have been introduced during the conversion of the source
1178 files to man page format. To report such errors, see https://www.ker‐
1179 nel.org/doc/man-pages/reporting_bugs.html .
1180
1181
1182
1183IEEE/The Open Group 2013 SH(1P)