1SH(1P)                     POSIX Programmer's Manual                    SH(1P)
2
3
4

PROLOG

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

NAME

12       sh - shell, the standard command language interpreter
13

SYNOPSIS

15       sh [-abCefhimnuvx][-o option][+abCefhimnuvx][+o option]
16               [command_file [argument...]]
17
18       sh -c[-abCefhimnuvx][-o option][+abCefhimnuvx][+o option]command_string
19               [command_name [argument...]]
20
21       sh -s[-abCefhimnuvx][-o option][+abCefhimnuvx][+o option][argument]
22
23

DESCRIPTION

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 Shell Command Lan‐
29       guage .
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

OPTIONS

37       The  sh  utility  shall  conform  to  the  Base  Definitions  volume of
38       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines, with  an
39       extension 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 Special  Built-In  Utilities  .
43       The  option letters derived from the set special built-in shall also be
44       accepted with a leading plus sign ( '+' ) instead of a  leading  hyphen
45       (meaning  the reverse case of the option as described in this volume of
46       IEEE Std 1003.1-2001).
47
48       The following additional options shall be supported:
49
50       -c     Read commands from the command_string operand. Set the value  of
51              special  parameter 0 (see Special Parameters ) from the value of
52              the command_name operand and the positional parameters ($1,  $2,
53              and  so on) in sequence from the remaining argument operands. No
54              commands shall be read from the standard input.
55
56       -i     Specify that the shell is interactive; see below. An implementa‐
57              tion  may treat specifying the -i option as an error if the real
58              user ID of the calling process does not equal the effective user
59              ID  or  if  the real group ID does not equal the effective group
60              ID.
61
62       -s     Read commands from the standard input.
63
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

OPERANDS

73       The following operands shall be supported:
74
75       -      A  single  hyphen shall be treated as the first operand and then
76              ignored.  If both '-' and "--" are given  as  arguments,  or  if
77              other  operands precede the single hyphen, the results are unde‐
78              fined.
79
80       argument
81              The positional parameters ($1, $2, and so on) shall  be  set  to
82              arguments, if any.
83
84       command_file
85              The pathname of a file containing commands. If the pathname con‐
86              tains one or more slash characters, the implementation  attempts
87              to read that file; the file need not be executable. If the path‐
88              name does not contain a slash character:
89
90               * The implementation shall attempt to read that file  from  the
91                 current working directory; the file need not be executable.
92
93               * If  the  file  is  not  in the current working directory, the
94                 implementation may perform a search for  an  executable  file
95                 using  the value of PATH,  as described in Command Search and
96                 Execution .
97
98       Special parameter 0 (see Special Parameters ) shall be set to the value
99       of  command_file. If sh is called using a synopsis form that omits com‐
100       mand_file, special parameter 0 shall be set to the value of  the  first
101       argument  passed  to  sh  from its parent (for example, argv[0] for a C
102       program), which is normally a pathname used to execute the sh utility.
103
104       command_name
105
106              A string assigned to special parameter 0 when executing the com‐
107              mands  in command_string. If command_name is not specified, spe‐
108              cial parameter 0 shall be set to the value of the first argument
109              passed  to sh from its parent (for example, argv[0] for a C pro‐
110              gram), which is normally a pathname used to execute the sh util‐
111              ity.
112
113       command_string
114
115              A  string  that shall be interpreted by the shell as one or more
116              commands, as if the string were the  argument  to  the  system()
117              function   defined   in   the   System   Interfaces   volume  of
118              IEEE Std 1003.1-2001.  If the command_string operand is an empty
119              string, sh shall exit with a zero exit status.
120
121

STDIN

123       The standard input shall be used only if one of the following is true:
124
125        * The -s option is specified.
126
127        * The -c option is not specified and no operands are specified.
128
129        * The  script  executes  one  or more commands that require input from
130          standard input (such as a read command that does  not  redirect  its
131          input).
132
133       See the INPUT FILES section.
134
135       When  the  shell  is using standard input and it invokes a command that
136       also uses standard input, the shell  shall  ensure  that  the  standard
137       input  file  pointer points directly after the command it has read when
138       the command begins execution. It shall not read ahead in such a  manner
139       that any characters intended to be read by the invoked command are con‐
140       sumed by the shell (whether interpreted by the shell or  not)  or  that
141       characters that are not read by the invoked command are not seen by the
142       shell. When the command expecting to read  standard  input  is  started
143       asynchronously by an interactive shell, it is unspecified whether char‐
144       acters are read by the command or interpreted by the shell.
145
146       If the standard input to sh is a FIFO or terminal device and is set  to
147       non-blocking  reads,  then  sh  shall enable blocking reads on standard
148       input. This shall remain in effect when the command completes.
149

INPUT FILES

151       The input file shall be a text file, except that line lengths shall  be
152       unlimited. If the input file is empty or consists solely of blank lines
153       or comments, or both, sh shall exit with a zero exit status.
154

ENVIRONMENT VARIABLES

156       The following environment variables shall affect the execution of sh:
157
158       ENV    This variable, when  and  only  when  an  interactive  shell  is
159              invoked,  shall be subjected to parameter expansion (see Parame‐
160              ter Expansion ) by the shell, and the resulting value  shall  be
161              used  as  a pathname of a file containing shell commands to exe‐
162              cute in the current environment.  The  file  need  not  be  exe‐
163              cutable.  If  the expanded value of ENV is not an absolute path‐
164              name, the results are unspecified.  ENV shall be ignored if  the
165              real  and  effective user IDs or real and effective group IDs of
166              the process are different.
167
168       FCEDIT This variable, when expanded by the shell, shall  determine  the
169              default value for the -e editor option's editor option-argument.
170              If FCEDIT is null or unset, ed shall be used as the editor. This
171              volume  of  IEEE Std 1003.1-2001  specifies  the effects of this
172              variable only for systems supporting the User Portability Utili‐
173              ties option.
174
175       HISTFILE
176              Determine a pathname naming a command history file. If the HIST‐
177              FILE variable is not set, the shell may  attempt  to  access  or
178              create  a  file  .sh_history in the directory referred to by the
179              HOME environment variable. If the shell cannot obtain both  read
180              and  write  access to, or create, the history file, it shall use
181              an unspecified mechanism that  allows  the  history  to  operate
182              properly. (References to history "file" in this section shall be
183              understood to mean this unspecified mechanism in such cases.) An
184              implementation may choose to access this variable only when ini‐
185              tializing the history file; this initialization shall occur when
186              fc  or sh first attempt to retrieve entries from, or add entries
187              to, the file, as the result of commands issued by the user,  the
188              file named by the ENV variable, or implementation-defined system
189              start-up files. Implementations may choose to disable  the  his‐
190              tory list mechanism for users with appropriate privileges who do
191              not set HISTFILE ; the specific circumstances under  which  this
192              occurs  are implementation-defined. If more than one instance of
193              the shell is using the same history file, it is unspecified  how
194              updates  to  the  history  file  from  those shells interact. As
195              entries are deleted from the history file, they shall be deleted
196              oldest  first.   It is unspecified when history file entries are
197              physically  removed  from  the  history  file.  This  volume  of
198              IEEE Std 1003.1-2001 specifies the effects of this variable only
199              for systems supporting the User Portability Utilities option.
200
201       HISTSIZE
202              Determine a decimal number representing the limit to the  number
203              of  previous  commands  that are accessible. If this variable is
204              unset, an unspecified default greater than or equal to 128 shall
205              be  used.  The maximum number of commands in the history list is
206              unspecified, but shall be at least 128.  An  implementation  may
207              choose  to  access this variable only when initializing the his‐
208              tory file,  as  described  under  HISTFILE.   Therefore,  it  is
209              unspecified  whether  changes made to HISTSIZE after the history
210              file has been initialized are effective.
211
212       HOME   Determine the pathname of the user's home  directory.  The  con‐
213              tents  of HOME are used in tilde expansion as described in Tilde
214              Expansion . This volume of  IEEE Std 1003.1-2001  specifies  the
215              effects  of  this  variable only for systems supporting the User
216              Portability Utilities option.
217
218       IFS    (Input Field Separators.) A string treated as a list of  charac‐
219              ters  that  shall be used for field splitting and to split lines
220              into words with the read command. See Field Splitting .  If  IFS
221              is  not  set, the shell shall behave as if the value of IFS were
222              <space>, <tab>, and <newline>. Implementations  may  ignore  the
223              value  of  IFS  in  the  environment  at the time sh is invoked,
224              treating IFS as if it were not set.
225
226       LANG   Provide a default value for the  internationalization  variables
227              that  are  unset  or  null.  (See the Base Definitions volume of
228              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
229              ables  for the precedence of internationalization variables used
230              to determine the values of locale categories.)
231
232       LC_ALL If set to a non-empty string value, override the values  of  all
233              the other internationalization variables.
234
235       LC_COLLATE
236
237              Determine   the   behavior  of  range  expressions,  equivalence
238              classes, and multi-character collating elements  within  pattern
239              matching.
240
241       LC_CTYPE
242              Determine  the  locale  for  the  interpretation of sequences of
243              bytes of text data as characters (for  example,  single-byte  as
244              opposed  to multi-byte characters in arguments and input files),
245              which characters are defined as letters (character class alpha),
246              and the behavior of character classes within pattern matching.
247
248       LC_MESSAGES
249              Determine  the  locale  that should be used to affect the format
250              and contents of diagnostic messages written to standard error.
251
252       MAIL   Determine a pathname of the user's mailbox file for purposes  of
253              incoming  mail  notification. If this variable is set, the shell
254              shall inform the user if the file named by the variable is  cre‐
255              ated or if its modification time has changed. Informing the user
256              shall be accomplished by writing a string of unspecified  format
257              to  standard  error  prior  to  the  writing of the next primary
258              prompt string. Such check shall be performed only after the com‐
259              pletion  of the interval defined by the MAILCHECK variable after
260              the last such check. The user shall be informed only if MAIL  is
261              set and MAILPATH is not set. This volume of IEEE Std 1003.1-2001
262              specifies the effects of this variable only for systems support‐
263              ing the User Portability Utilities option.
264
265       MAILCHECK
266
267              Establish  a  decimal integer value that specifies how often (in
268              seconds) the shell shall check for the arrival of  mail  in  the
269              files  specified  by the MAILPATH or MAIL variables. The default
270              value shall be 600 seconds. If set  to  zero,  the  shell  shall
271              check  before  issuing  each  primary  prompt.  This  volume  of
272              IEEE Std 1003.1-2001 specifies the effects of this variable only
273              for systems supporting the User Portability Utilities option.
274
275       MAILPATH
276              Provide  a  list of pathnames and optional messages separated by
277              colons.  If this variable is set, the  shell  shall  inform  the
278              user if any of the files named by the variable are created or if
279              any of their modification times change. (See the preceding entry
280              for  MAIL  for descriptions of mail arrival and user informing.)
281              Each pathname can be followed by '%' and a string that shall  be
282              subjected  to  parameter expansion and written to standard error
283              when the modification time changes. If a '%'  character  in  the
284              pathname  is  preceded  by a backslash, it shall be treated as a
285              literal '%' in the pathname. The default message is unspecified.
286
287       The MAILPATH environment variable takes precedence over the MAIL  vari‐
288       able. This volume of IEEE Std 1003.1-2001 specifies the effects of this
289       variable only for systems supporting  the  User  Portability  Utilities
290       option.
291
292       NLSPATH
293              Determine the location of message catalogs for the processing of
294              LC_MESSAGES .
295
296       PATH   Establish a string formatted as described in  the  Base  Defini‐
297              tions  volume  of  IEEE Std 1003.1-2001,  Chapter 8, Environment
298              Variables, used to effect command  interpretation;  see  Command
299              Search and Execution .
300
301       PWD    This  variable  shall represent an absolute pathname of the cur‐
302              rent working directory. Assignments  to  this  variable  may  be
303              ignored  unless the value is an absolute pathname of the current
304              working directory and there are no filename components of dot or
305              dot-dot.
306
307

ASYNCHRONOUS EVENTS

309       Default.
310

STDOUT

312       See the STDERR section.
313

STDERR

315       Except  as  otherwise stated (by the descriptions of any invoked utili‐
316       ties or in interactive mode), standard error shall  be  used  only  for
317       diagnostic messages.
318

OUTPUT FILES

320       None.
321

EXTENDED DESCRIPTION

323       See  Shell Command Language . The following additional capabilities are
324       supported on systems supporting the User Portability Utilities option.
325
326   Command History List
327       When the sh utility is being used interactively, it  shall  maintain  a
328       list of commands previously entered from the terminal in the file named
329       by the HISTFILE environment variable.  The  type,  size,  and  internal
330       format  of  this  file are unspecified. Multiple sh processes can share
331       access to the file for a user, if file access permissions  allow  this;
332       see the description of the HISTFILE environment variable.
333
334   Command Line Editing
335       When  sh  is being used interactively from a terminal, the current com‐
336       mand and the command history (see fc ) can be edited using vi-mode com‐
337       mand line editing. This mode uses commands, described below, similar to
338       a subset of those described in the  vi  utility.   Implementations  may
339       offer  other  command line editing modes corresponding to other editing
340       utilities.
341
342       The command set -o vi shall enable vi-mode editing and place sh into vi
343       insert  mode  (see  Command Line Editing (vi-mode) ). This command also
344       shall disable any other editing mode that the implementation  may  pro‐
345       vide. The command set +o vi disables vi-mode editing.
346
347       Certain  block-mode  terminals  may  be unable to support shell command
348       line editing. If a terminal is unable to provide either edit  mode,  it
349       need  not  be possible to set -o vi when using the shell on this termi‐
350       nal.
351
352       In the following sections, the characters erase, interrupt,  kill,  and
353       end-of-file are those set by the stty utility.
354
355   Command Line Editing (vi-mode)
356       In vi editing mode, there shall be a distinguished line, the edit line.
357       All the editing operations which modify a line affect  the  edit  line.
358       The edit line is always the newest line in the command history buffer.
359
360       With  vi-mode  enabled, sh can be switched between insert mode and com‐
361       mand mode.
362
363       When in insert mode, an entered character shall be  inserted  into  the
364       command  line,  except  as  noted in vi Line Editing Insert Mode . Upon
365       entering sh and after termination of the previous command, sh shall  be
366       in insert mode.
367
368       Typing  an  escape  character shall switch sh into command mode (see vi
369       Line Editing Command Mode ). In  command  mode,  an  entered  character
370       shall  either  invoke  a defined operation, be used as part of a multi-
371       character operation, or be treated as an error. A character that is not
372       recognized  as  part of an editing command shall terminate any specific
373       editing command and shall alert  the  terminal.  Typing  the  interrupt
374       character  in  command  mode  shall  cause sh to terminate command line
375       editing on the current command line, reissue the  prompt  on  the  next
376       line  of  the terminal, and reset the command history (see fc ) so that
377       the most recently executed command is the previous  command  (that  is,
378       the  command that was being edited when it was interrupted is not reen‐
379       tered into the history).
380
381       In the following sections, the phrase "move the cursor to the beginning
382       of  the word" shall mean "move the cursor to the first character of the
383       current word" and the phrase "move the cursor to the end of  the  word"
384       shall mean "move the cursor to the last character of the current word".
385       The phrase "beginning of the command line" indicates the point  between
386       the  end  of the prompt string issued by the shell (or the beginning of
387       the terminal line, if there is no prompt string) and the first  charac‐
388       ter of the command text.
389
390   vi Line Editing Insert Mode
391       While in insert mode, any character typed shall be inserted in the cur‐
392       rent command line, unless it is from the following set.
393
394       <newline>
395              Execute the current command line. If the current command line is
396              not  empty,  this line shall be entered into the command history
397              (see fc ).
398
399       erase  Delete the character previous to the current cursor position and
400              move  the  current cursor position back one character. In insert
401              mode, characters shall be erased from both the  screen  and  the
402              buffer when backspacing.
403
404       interrupt
405              Terminate   command  line  editing  with  the  same  effects  as
406              described for interrupting command mode; see Command Line  Edit‐
407              ing (vi-mode) .
408
409       kill   Clear all the characters from the input line.
410
411       <control>-V
412              Insert the next character input, even if the character is other‐
413              wise a special insert mode character.
414
415       <control>-W
416              Delete the characters from the one preceding the cursor  to  the
417              preceding  word  boundary. The word boundary in this case is the
418              closer to the cursor of either the beginning of the  line  or  a
419              character that is in neither the blank nor punct character clas‐
420              sification of the current locale.
421
422       end-of-file
423              Interpreted as the end of input in sh. This interpretation shall
424              occur  only at the beginning of an input line. If end-of-file is
425              entered other than at the beginning of the line, the results are
426              unspecified.
427
428       <ESC>  Place sh into command mode.
429
430
431   vi Line Editing Command Mode
432       In  command  mode  for the command line editing feature, decimal digits
433       not beginning with 0 that precede a command letter shall be remembered.
434       Some  commands  use these decimal digits as a count number that affects
435       the operation.
436
437       The term motion command represents one of the commands:
438
439
440              <space>  0  b  F  l  W  ^  $  ;  E  f  T  w  |  ,  B  e  h  t
441
442       If the current line is not the edit line, any command that modifies the
443       current line shall cause the content of the current line to replace the
444       content of the edit line, and the current line shall  become  the  edit
445       line.  This  replacement  cannot  be  undone  (see the u and U commands
446       below). The modification requested shall then be performed to the  edit
447       line. When the current line is the edit line, the modification shall be
448       done directly to the edit line.
449
450       Any command that is preceded by count shall take a count  (the  numeric
451       value  of  any  preceding decimal digits). Unless otherwise noted, this
452       count shall cause the specified operation to repeat by  the  number  of
453       times specified by the count. Also unless otherwise noted, a count that
454       is out of range is considered an error condition and  shall  alert  the
455       terminal,  but neither the cursor position, nor the command line, shall
456       change.
457
458       The terms word and bigword are used as defined in the  vi  description.
459       The term save buffer corresponds to the term unnamed buffer in vi.
460
461       The following commands shall be recognized in command mode:
462
463       <newline>
464              Execute the current command line. If the current command line is
465              not empty, this line shall be entered into the  command  history
466              (see fc ).
467
468       <control>-L
469              Redraw the current command line. Position the cursor at the same
470              location on the redrawn line.
471
472       #      Insert the character '#' at the beginning of the current command
473              line  and treat the resulting edit line as a comment.  This line
474              shall be entered into the command history; see fc .
475
476       =      Display the possible shell word expansions (see Word  Expansions
477              ) of the bigword at the current command line position.
478
479       Note:
480              This does not modify the content of the current line, and there‐
481              fore does not cause the current line to become the edit line.
482
483
484       These expansions shall be displayed on subsequent terminal  lines.   If
485       the bigword contains none of the characters '?', '*', or '[', an aster‐
486       isk ( '*' ) shall be implicitly assumed at the end. If any  directories
487       are  matched,  these  expansions  shall  have a '/' character appended.
488       After the expansion, the line shall be redrawn, the cursor repositioned
489       at the current cursor position, and sh shall be placed in command mode.
490
491       \      Perform pathname expansion (see Pathname Expansion ) on the cur‐
492              rent bigword, up to the largest set of characters  that  can  be
493              matched  uniquely.   If the bigword contains none of the charac‐
494              ters '?', '*', or '[', an asterisk ( '*' ) shall  be  implicitly
495              assumed  at  the  end. This maximal expansion then shall replace
496              the original bigword in the command line, and the  cursor  shall
497              be  placed  after  this expansion. If the resulting bigword com‐
498              pletely and uniquely matches a directory, a '/' character  shall
499              be  inserted  directly  after the bigword. If some other file is
500              completely matched, a single <space> shall be inserted after the
501              bigword.  After  this  operation,  sh  shall be placed in insert
502              mode.
503
504       *      Perform pathname expansion on the current bigword and insert all
505              expansions into the command to replace the current bigword, with
506              each expansion separated by a single <space>. If at the  end  of
507              the  line,  the  current  cursor  position shall be moved to the
508              first column position following the expansions and sh  shall  be
509              placed  in  insert  mode. Otherwise, the current cursor position
510              shall be the last column position of the first  character  after
511              the  expansions  and  sh  shall be placed in insert mode. If the
512              current bigword contains none of the  characters  '?',  '*',  or
513              '[',  before  the  operation,  an  asterisk  shall be implicitly
514              assumed at the end.
515
516       @letter
517              Insert the value of the alias named _letter. The  symbol  letter
518              represents a single alphabetic character from the portable char‐
519              acter set; implementations may support additional characters  as
520              an  extension.  If the alias _letter contains other editing com‐
521              mands, these commands shall be performed as part of  the  inser‐
522              tion. If no alias _letter is enabled, this command shall have no
523              effect.
524
525       [count]~
526              Convert, if the current character is a lowercase letter, to  the
527              equivalent uppercase letter and vice versa, as prescribed by the
528              current locale.  The  current  cursor  position  then  shall  be
529              advanced  by  one character. If the cursor was positioned on the
530              last character of the line, the case conversion shall occur, but
531              the  cursor shall not advance. If the '~' command is preceded by
532              a count, that number of characters shall be converted,  and  the
533              cursor  shall  be  advanced  to the character position after the
534              last character converted. If the count is larger than the number
535              of  characters after the cursor, this shall not be considered an
536              error; the cursor shall advance to the  last  character  on  the
537              line.
538
539       [count].
540              Repeat  the  most recent non-motion command, even if it was exe‐
541              cuted on an earlier command line. If the  previous  command  was
542              preceded  by a count, and no count is given on the '.'  command,
543              the count from the previous command shall be included as part of
544              the repeated command. If the '.' command is preceded by a count,
545              this shall override any count argument to the previous  command.
546              The  count  specified  in the '.' command shall become the count
547              for subsequent '.' commands issued without a count.
548
549       [number]v
550              Invoke the vi editor to edit the current command line in a  tem‐
551              porary  file.  When the editor exits, the commands in the tempo‐
552              rary file shall be executed and placed in the  command  history.
553              If  a number is included, it specifies the command number in the
554              command history to be edited, rather than  the  current  command
555              line.
556
557       [count]l   (ell)
558
559       [count]<space>
560
561              Move the current cursor position to the next character position.
562              If the cursor was positioned on the last character of the  line,
563              the  terminal  shall  be  alerted  and  the  cursor shall not be
564              advanced.  If the count is larger than the number of  characters
565              after  the  cursor,  this  shall not be considered an error; the
566              cursor shall advance to the last character on the line.
567
568       [count]h
569              Move the current cursor position to the countth (default 1) pre‐
570              vious  character  position.  If the cursor was positioned on the
571              first character of the line, the terminal shall be  alerted  and
572              the  cursor  shall not be moved. If the count is larger than the
573              number of characters before the cursor, this shall not  be  con‐
574              sidered  an  error; the cursor shall move to the first character
575              on the line.
576
577       [count]w
578              Move to the start of the next word. If the cursor was positioned
579              on the last character of the line, the terminal shall be alerted
580              and the cursor shall not be advanced. If  the  count  is  larger
581              than  the  number  of  words after the cursor, this shall not be
582              considered an error; the cursor shall advance to the last  char‐
583              acter on the line.
584
585       [count]W
586              Move  to  the start of the next bigword. If the cursor was posi‐
587              tioned on the last character of the line, the terminal shall  be
588              alerted  and  the  cursor shall not be advanced. If the count is
589              larger than the number of bigwords after the cursor, this  shall
590              not be considered an error; the cursor shall advance to the last
591              character on the line.
592
593       [count]e
594              Move to the end of the current word. If at the end  of  a  word,
595              move  to  the end of the next word. If the cursor was positioned
596              on the last character of the line, the terminal shall be alerted
597              and  the  cursor  shall  not be advanced. If the count is larger
598              than the number of words after the cursor,  this  shall  not  be
599              considered  an error; the cursor shall advance to the last char‐
600              acter on the line.
601
602       [count]E
603              Move to the end of the current bigword. If at the end of a  big‐
604              word,  move  to  the  end of the next bigword. If the cursor was
605              positioned on the last character of the line, the terminal shall
606              be alerted and the cursor shall not be advanced. If the count is
607              larger than the number of bigwords after the cursor, this  shall
608              not be considered an error; the cursor shall advance to the last
609              character on the line.
610
611       [count]b
612              Move to the beginning of the current word. If at  the  beginning
613              of  a  word,  move to the beginning of the previous word. If the
614              cursor was positioned on the first character of  the  line,  the
615              terminal  shall be alerted and the cursor shall not be moved. If
616              the count is larger than the number of words preceding the  cur‐
617              sor,  this  shall  not  be considered an error; the cursor shall
618              return to the first character on the line.
619
620       [count]B
621              Move to the beginning of the current bigword. If at  the  begin‐
622              ning  of  a  bigword, move to the beginning of the previous big‐
623              word.  If the cursor was positioned on the  first  character  of
624              the line, the terminal shall be alerted and the cursor shall not
625              be moved.  If the count is larger than the  number  of  bigwords
626              preceding the cursor, this shall not be considered an error; the
627              cursor shall return to the first character on the line.
628
629       ^      Move the current cursor position to the first character  on  the
630              input line that is not a <blank>.
631
632       $      Move to the last character position on the current command line.
633
634       0      (Zero.) Move to the first character position on the current com‐
635              mand line.
636
637       [count]|
638              Move to the countth character position on  the  current  command
639              line. If no number is specified, move to the first position. The
640              first character position shall be numbered 1. If  the  count  is
641              larger than the number of characters on the line, this shall not
642              be considered an error; the cursor shall be placed on  the  last
643              character on the line.
644
645       [count]fc
646              Move  to  the  first occurrence of the character 'c' that occurs
647              after the current cursor position. If the cursor was  positioned
648              on the last character of the line, the terminal shall be alerted
649              and the cursor shall not be advanced. If the character 'c'  does
650              not  occur  in  the  line after the current cursor position, the
651              terminal shall be alerted and the cursor shall not be moved.
652
653       [count]Fc
654              Move to the first occurrence of the character  'c'  that  occurs
655              before the current cursor position. If the cursor was positioned
656              on the first character  of  the  line,  the  terminal  shall  be
657              alerted  and the cursor shall not be moved. If the character 'c'
658              does not occur in the line before the current  cursor  position,
659              the terminal shall be alerted and the cursor shall not be moved.
660
661       [count]tc
662              Move to the character before the first occurrence of the charac‐
663              ter 'c' that occurs after the current cursor position.   If  the
664              cursor  was  positioned  on  the last character of the line, the
665              terminal shall be alerted and the cursor shall not be  advanced.
666              If  the  character 'c' does not occur in the line after the cur‐
667              rent cursor position, the terminal shall be alerted and the cur‐
668              sor shall not be moved.
669
670       [count]Tc
671              Move  to the character after the first occurrence of the charac‐
672              ter 'c' that occurs before the current cursor position.  If  the
673              cursor  was  positioned  on the first character of the line, the
674              terminal shall be alerted and the cursor shall not be moved.  If
675              the  character 'c' does not occur in the line before the current
676              cursor position, the terminal shall be alerted  and  the  cursor
677              shall not be moved.
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. Errors are those
682              described for the repeated command.
683
684       [count],
685              Repeat  the most recent f, F, t, or T command.  Any number argu‐
686              ment on that previous command shall be ignored. However, reverse
687              the direction of that command.
688
689       a      Enter  insert mode after the current cursor position. Characters
690              that are entered shall be inserted before the next character.
691
692       A      Enter insert mode after the end of the current command line.
693
694       i      Enter insert mode at the  current  cursor  position.  Characters
695              that are entered shall be inserted before the current character.
696
697       I      Enter insert mode at the beginning of the current command line.
698
699       R      Enter  insert  mode,  replacing characters from the command line
700              beginning at the current cursor position.
701
702       [count]cmotion
703
704              Delete the characters between the current  cursor  position  and
705              the  cursor position that would result from the specified motion
706              command. Then enter insert mode before the first character  fol‐
707              lowing  any  deleted characters. If count is specified, it shall
708              be applied to the motion command. A count shall be  ignored  for
709              the following motion commands:
710
711
712              0    ^    $    c
713
714       If  the  motion  command is the character 'c', the current command line
715       shall be cleared and insert mode shall be entered. If the  motion  com‐
716       mand would move the current cursor position toward the beginning of the
717       command line, the character under the current cursor position shall not
718       be  deleted.  If the motion command would move the current cursor posi‐
719       tion toward the end of the command line, the character under  the  cur‐
720       rent  cursor position shall be deleted. If the count is larger than the
721       number of characters between the current cursor position and the end of
722       the command line toward which the motion command would move the cursor,
723       this shall not be considered an error; all of the remaining  characters
724       in  the  aforementioned range shall be deleted and insert mode shall be
725       entered. If the motion  command  is  invalid,  the  terminal  shall  be
726       alerted, the cursor shall not be moved, and no text shall be deleted.
727
728       C      Delete  from  the  current  character to the end of the line and
729              enter insert mode at the new end-of-line.
730
731       S      Clear the entire edit line and enter insert mode.
732
733       [count]rc
734              Replace the current character with the character 'c'  .  With  a
735              number  count,  replace  the  current  and the following count-1
736              characters. After this  command,  the  current  cursor  position
737              shall be on the last character that was changed. If the count is
738              larger than the number of  characters  after  the  cursor,  this
739              shall  not  be considered an error; all of the remaining charac‐
740              ters shall be changed.
741
742       [count]_
743              Append a <space> after the current character position  and  then
744              append  the  last  bigword  in the previous input line after the
745              <space>. Then enter insert mode after the  last  character  just
746              appended. With a number count, append the countth bigword in the
747              previous line.
748
749       [count]x
750              Delete the character at the current cursor  position  and  place
751              the  deleted  characters  in  the save buffer. If the cursor was
752              positioned on the last character  of  the  line,  the  character
753              shall  be  deleted and the cursor position shall be moved to the
754              previous character (the new last character).  If  the  count  is
755              larger  than  the  number  of  characters after the cursor, this
756              shall not be considered an error; all the  characters  from  the
757              cursor to the end of the line shall be deleted.
758
759       [count]X
760              Delete  the  character  before  the  current cursor position and
761              place the deleted characters in the save buffer.  The  character
762              under  the current cursor position shall not change. If the cur‐
763              sor was positioned on the first character of the line, the  ter‐
764              minal  shall be alerted, and the X command shall have no effect.
765              If the line contained a single character, the  X  command  shall
766              have  no effect. If the line contained no characters, the termi‐
767              nal shall be alerted and the cursor shall not be moved.  If  the
768              count is larger than the number of characters before the cursor,
769              this shall not be considered an error; all the  characters  from
770              before the cursor to the beginning of the line shall be deleted.
771
772       [count]dmotion
773
774              Delete  the  characters  between the current cursor position and
775              the character position that would result from  the  motion  com‐
776              mand.  A number count repeats the motion command count times. If
777              the motion command would move toward the beginning of  the  com‐
778              mand line, the character under the current cursor position shall
779              not be deleted. If the motion command is d, the  entire  current
780              command  line  shall be cleared. If the count is larger than the
781              number of characters between the current cursor position and the
782              end  of  the  command line toward which the motion command would
783              move the cursor, this shall not be considered an error;  all  of
784              the  remaining  characters  in the aforementioned range shall be
785              deleted. The deleted characters shall be placed in the save buf‐
786              fer.
787
788       D      Delete  all  characters  from the current cursor position to the
789              end of the line. The deleted characters shall be placed  in  the
790              save buffer.
791
792       [count]ymotion
793
794              Yank  (that  is,  copy)  the  characters from the current cursor
795              position to the position resulting from the motion command  into
796              the  save  buffer. A number count shall be applied to the motion
797              command.  If the motion command would move toward the  beginning
798              of  the  command  line,  the  character under the current cursor
799              position shall not be included in the set of yanked  characters.
800              If  the  motion  command  is  y, the entire current command line
801              shall be yanked into the save buffer. The current  cursor  posi‐
802              tion  shall be unchanged. If the count is larger than the number
803              of characters between the current cursor position and the end of
804              the  command line toward which the motion command would move the
805              cursor, this shall not  be  considered  an  error;  all  of  the
806              remaining  characters  in  the  aforementioned  range  shall  be
807              yanked.
808
809       Y      Yank the characters from the current cursor position to the  end
810              of the line into the save buffer. The current character position
811              shall be unchanged.
812
813       [count]p
814              Put a copy of the current contents of the save buffer after  the
815              current  cursor  position.  The current cursor position shall be
816              advanced to the last character put from the save buffer. A count
817              shall indicate how many copies of the save buffer shall be put.
818
819       [count]P
820              Put a copy of the current contents of the save buffer before the
821              current cursor position. The current cursor  position  shall  be
822              moved  to  the  last character put from the save buffer. A count
823              shall indicate how many copies of the save buffer shall be put.
824
825       u      Undo the last command that changed the edit line. This operation
826              shall not undo the copy of any command line to the edit line.
827
828       U      Undo all changes made to the edit line. This operation shall not
829              undo the copy of any command line to the edit line.
830
831       [count]k
832
833       [count]-
834              Set the current command line to be the countth previous  command
835              line in the shell command history. If count is not specified, it
836              shall default to 1. The cursor shall be positioned on the  first
837              character  of the new command. If a k or - command would retreat
838              past the maximum number of commands in  effect  for  this  shell
839              (affected  by  the  HISTSIZE environment variable), the terminal
840              shall be alerted, and the command shall have no effect.
841
842       [count]j
843
844       [count]+
845              Set the current command line to be the countth next command line
846              in  the  shell  command  history.  If count is not specified, it
847              shall default to 1. The cursor shall be positioned on the  first
848              character  of the new command. If a j or + command advances past
849              the edit line, the current command line shall be restored to the
850              edit line and the terminal shall be alerted.
851
852       [number]G
853              Set  the  current  command  line  to  be the oldest command line
854              stored in the shell command history. With a number  number,  set
855              the  current  command  line to be the command line number in the
856              history. If command line number does  not  exist,  the  terminal
857              shall be alerted and the command line shall not be changed.
858
859       /pattern<newline>
860
861              Move  backwards  through  the command history, searching for the
862              specified pattern, beginning with  the  previous  command  line.
863              Patterns  use the pattern matching notation described in Pattern
864              Matching Notation, except that the '^' character shall have spe‐
865              cial  meaning when it appears as the first character of pattern.
866              In this case, the '^' is discarded and the characters after  the
867              '^'  shall  be matched only at the beginning of a line. Commands
868              in the command history shall be treated as strings, not as file‐
869              names.   If  the  pattern is not found, the current command line
870              shall be unchanged and the terminal is alerted. If it  is  found
871              in  a  previous  line,  the current command line shall be set to
872              that line and the cursor shall be set to the first character  of
873              the new command line.
874
875       If  pattern  is  empty,  the  last non-empty pattern provided to / or ?
876       shall be used. If there is no previous non-empty pattern, the  terminal
877       shall be alerted and the current command line shall remain unchanged.
878
879       ?pattern<newline>
880
881              Move  forwards  through  the  command history, searching for the
882              specified pattern, beginning with the next  command  line.  Pat‐
883              terns  use  the  pattern  matching notation described in Pattern
884              Matching Notation, except that the '^' character shall have spe‐
885              cial  meaning when it appears as the first character of pattern.
886              In this case, the '^' is discarded and the characters after  the
887              '^'  shall  be matched only at the beginning of a line. Commands
888              in the command history shall be treated as strings, not as file‐
889              names.   If  the  pattern is not found, the current command line
890              shall be unchanged and the terminal alerted. If it is found in a
891              following  line,  the  current command line shall be set to that
892              line and the cursor shall be set to the fist  character  of  the
893              new command line.
894
895       If  pattern  is  empty,  the  last non-empty pattern provided to / or ?
896       shall be used. If there is no previous non-empty pattern, the  terminal
897       shall be alerted and the current command line shall remain unchanged.
898
899       n      Repeat the most recent / or ? command. If there is no previous /
900              or ?, the terminal shall be alerted and the current command line
901              shall remain unchanged.
902
903       N      Repeat  the  most recent / or ? command, reversing the direction
904              of the search. If there is no previous  /  or  ?,  the  terminal
905              shall  be  alerted  and  the  current  command line shall remain
906              unchanged.
907
908

EXIT STATUS

910       The following exit values shall be returned:
911
912           0  The script to be executed consisted solely of zero or more blank
913              lines or comments, or both.
914
915       1-125  A non-interactive shell detected a syntax, redirection, or vari‐
916              able assignment error.
917
918         127  A specified command_file could not be found by a non-interactive
919              shell.
920
921
922       Otherwise,  the  shell shall return the exit status of the last command
923       it invoked or attempted to invoke (see also the exit utility in Special
924       Built-In Utilities ).
925

CONSEQUENCES OF ERRORS

927       See Consequences of Shell Errors .
928
929       The following sections are informative.
930

APPLICATION USAGE

932       Standard  input and standard error are the files that determine whether
933       a shell is interactive when -i is not specified.  For example:
934
935
936              sh > file
937
938       and:
939
940
941              sh 2> file
942
943       create interactive and non-interactive shells,  respectively.  Although
944       both accept terminal input, the results of error conditions are differ‐
945       ent, as described in Consequences of Shell Errors ; in the second exam‐
946       ple  a  redirection  error  encountered  by  a special built-in utility
947       aborts the shell.
948
949       A conforming application must protect its first operand, if  it  starts
950       with  a  plus sign, by preceding it with the "--" argument that denotes
951       the end of the options.
952
953       Applications should note that the standard PATH to the shell cannot  be
954       assumed  to  be either /bin/sh or /usr/bin/sh, and should be determined
955       by interrogation of the PATH returned by getconf PATH,   ensuring  that
956       the returned pathname is an absolute pathname and not a shell built-in.
957
958       For example, to determine the location of the standard sh utility:
959
960
961              command -v sh
962
963       On some implementations this might return:
964
965
966              /usr/xpg4/bin/sh
967
968       Furthermore, on systems that support executable scripts (the "#!"  con‐
969       struct), it is recommended that applications using  executable  scripts
970       install  them  using  getconf  -v  to  determine the shell pathname and
971       update the "#!" script appropriately as  it  is  being  installed  (for
972       example, with sed). For example:
973
974
975              #
976              # Installation time script to install correct POSIX shell pathname
977              #
978              # Get list of paths to check
979              #
980              Sifs=$IFS
981              IFS=:
982              set $(getconf PATH)
983              IFS=$Sifs
984              #
985              # Check each path for 'sh'
986              #
987              for i in $@
988              do
989                  if [ -f ${i}/sh ];
990                  then
991                      Pshell=${i}/sh
992                  fi
993              done
994              #
995              # This is the list of scripts to update. They should be of the
996              # form '${name}.source' and will be transformed to '${name}'.
997              # Each script should begin:
998              #
999              # !INSTALLSHELLPATH -p
1000              #
1001              scripts="a b c"
1002              #
1003              # Transform each script
1004              #
1005              for i in ${scripts}
1006              do
1007                  sed -e "s|INSTALLSHELLPATH|${Pshell}|" < ${i}.source > ${i}
1008              done
1009

EXAMPLES

1011        1. Execute a shell command from a string:
1012
1013
1014           sh -c "cat myfile"
1015
1016        2. Execute a shell script from a file in the current directory:
1017
1018
1019           sh my_shell_cmds
1020

RATIONALE

1022       The  sh utility and the set special built-in utility share a common set
1023       of options.
1024
1025       The KornShell ignores the contents of IFS upon entry to the  script.  A
1026       conforming application cannot rely on importing IFS.  One justification
1027       for this, beyond security considerations, is to assist possible  future
1028       shell  compilers. Allowing IFS to be imported from the environment pre‐
1029       vents many optimizations that might otherwise be performed via dataflow
1030       analysis of the script itself.
1031
1032       The  text  in  the  STDIN  section about non-blocking reads concerns an
1033       instance of sh that has been invoked, probably by a C-language program,
1034       with standard input that has been opened using the O_NONBLOCK flag; see
1035       open() in the System Interfaces volume of IEEE Std 1003.1-2001. If  the
1036       shell  did  not reset this flag, it would immediately terminate because
1037       no input data would be available yet and that would be  considered  the
1038       same as end-of-file.
1039
1040       The  options  associated  with a restricted shell (command name rsh and
1041       the -r option) were excluded because the standard developers considered
1042       that  the  implied level of security could not be achieved and they did
1043       not want to raise false expectations.
1044
1045       On systems that support set-user-ID scripts, a historical trapdoor  has
1046       been  to  link a script to the name -i. When it is called by a sequence
1047       such as:
1048
1049
1050              sh -
1051
1052       or by:
1053
1054
1055              #! usr/bin/sh -
1056
1057       the historical systems have assumed  that  no  option  letters  follow.
1058       Thus,  this  volume of IEEE Std 1003.1-2001 allows the single hyphen to
1059       mark the end of the options, in addition to the use of the regular "--"
1060       argument, because it was considered that the older practice was so per‐
1061       vasive. An alternative approach is taken by the KornShell,  where  real
1062       and  effective user/group IDs must match for an interactive shell; this
1063       behavior    is    specifically    allowed    by    this    volume    of
1064       IEEE Std 1003.1-2001.
1065
1066       Note:  There  are  other problems with set-user-ID scripts that the two
1067              approaches described here do not resolve.
1068
1069
1070       The initialization process for the history file can be dependent on the
1071       system  start-up  files,  in that they may contain commands that effec‐
1072       tively preempt the user's settings of HISTFILE and HISTSIZE.  For exam‐
1073       ple,  function  definition  commands  are recorded in the history file,
1074       unless the set -o nolog option is  set.  If  the  system  administrator
1075       includes  function  definitions  in  some  system  start-up file called
1076       before the ENV file, the history file is initialized  before  the  user
1077       gets  a  chance  to  influence  its characteristics. In some historical
1078       shells, the history file is initialized just after  the  ENV  file  has
1079       been   processed.   Therefore,  it  is  implementation-defined  whether
1080       changes made to HISTFILE after the history file  has  been  initialized
1081       are effective.
1082
1083       The default messages for the various MAIL -related messages are unspec‐
1084       ified because they vary across implementations.  Typical messages are:
1085
1086
1087              "you have mail\n"
1088
1089       or:
1090
1091
1092              "you have new mail\n"
1093
1094       It is important that the descriptions of command line editing refer  to
1095       the  same  shell  as  that  in IEEE Std 1003.1-2001 so that interactive
1096       users can also be application programmers without having to  deal  with
1097       programmatic  differences  in their two environments. It is also essen‐
1098       tial that the utility name sh be specified because this explicit  util‐
1099       ity  name  is  too  firmly rooted in historical practice of application
1100       programs for it to change.
1101
1102       Consideration was given to mandating a diagnostic message when attempt‐
1103       ing  to set vi-mode on terminals that do not support command line edit‐
1104       ing. However, it is not historical practice for the shell  to  be  cog‐
1105       nizant  of  all terminal types and thus be able to detect inappropriate
1106       terminals in all cases.  Implementations are encouraged to supply diag‐
1107       nostics in this case whenever possible, rather than leaving the user in
1108       a state where editing commands work incorrectly.
1109
1110       In early proposals, the KornShell-derived emacs mode  of  command  line
1111       editing  was included, even though the emacs editor itself was not. The
1112       community of emacs proponents was adamant that the  full  emacs  editor
1113       not  be  standardized  because  they  were concerned that an attempt to
1114       standardize this very powerful environment would encourage  vendors  to
1115       ship strictly conforming versions lacking the extensibility required by
1116       the community.  The author of the original emacs program also expressed
1117       his  desire  to  omit  the program. Furthermore, there were a number of
1118       historical systems that did not include emacs, or included  it  without
1119       supporting it, but there were very few that did not include and support
1120       vi. The shell emacs command  line  editing  mode  was  finally  omitted
1121       because  it  became  apparent that the KornShell version and the editor
1122       being distributed with the GNU system had diverged  in  some  respects.
1123       The  author  of  emacs  requested  that  the POSIX emacs mode either be
1124       deleted  or  have  a  significant  number  of  unspecified  conditions.
1125       Although  the  KornShell author agreed to consider changes to bring the
1126       shell into alignment, the standard developers decided to defer specifi‐
1127       cation at that time. At the time, it was assumed that convergence on an
1128       acceptable definition would occur for a subsequent draft, but that  has
1129       not happened, and there appears to be no impetus to do so. In any case,
1130       implementations are free to offer additional command line editing modes
1131       based  on  the exact models of editors their users are most comfortable
1132       with.
1133
1134       Early proposals had the following list entry in vi Line Editing  Insert
1135       Mode :
1136
1137       \      If followed by the erase or kill character, that character shall
1138              be inserted into the input line. Otherwise, the backslash itself
1139              shall be inserted into the input line.
1140
1141
1142       However,  this  is  not  actually  a feature of sh command line editing
1143       insert mode, but one of some historical  terminal  line  drivers.  Some
1144       conforming  implementations  continue  to  do this when the stty iexten
1145       flag is set.
1146

FUTURE DIRECTIONS

1148       None.
1149

SEE ALSO

1151       Shell Command Language, cd, echo, exit(), fc, pwd, read(),  set,  stty,
1152       test,    umask(),    vi,    the    System    Interfaces    volume    of
1153       IEEE Std 1003.1-2001, dup(), exec, exit(), fork(), open(), pipe(), sig‐
1154       nal(), system(), ulimit(), umask(), wait()
1155
1157       Portions  of  this text are reprinted and reproduced in electronic form
1158       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
1159       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
1160       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
1161       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
1162       event of any discrepancy between this version and the original IEEE and
1163       The  Open Group Standard, the original IEEE and The Open Group Standard
1164       is the referee document. The original Standard can be  obtained  online
1165       at http://www.opengroup.org/unix/online.html .
1166
1167
1168
1169IEEE/The Open Group                  2003                               SH(1P)
Impressum