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