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