loksh-noxz

[fork] a Linux port of OpenBSD's ksh
git clone https://noxz.tech/git/loksh-noxz.git
Log | Files | README

sh.1
1.\"	$OpenBSD: sh.1,v 1.156 2022/12/19 08:19:50 sdk Exp $
2.\"
3.\" Copyright (c) 2015 Jason McIntyre <jmc@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: December 19 2022 $
18.Dt SH 1
19.Os
20.Sh NAME
21.Nm sh
22.Nd command language interpreter
23.Sh SYNOPSIS
24.Nm sh
25.Op Fl abCefhimnuvx
26.Op Fl o Ar option
27.Op Fl c Ar string | Fl s | Ar file
28.Sh DESCRIPTION
29The
30.Nm
31utility is a
32.Em command language interpreter :
33it reads one or more commands,
34either from the command line or from a file
35(a shell script),
36and then sets about executing those commands.
37Thus it is the
38main interface between the user and the operating system.
39.Pp
40This version of
41.Nm
42is actually
43.Nm ksh
44in disguise.
45As such, it also supports the features described in
46.Xr ksh 1 .
47This manual page describes only the parts
48relevant to a POSIX compliant
49.Nm .
50If portability is a concern,
51use only those features described in this page.
52.Pp
53The shell receives input as follows:
54.Pp
55.Bl -tag -width "-c stringXXX" -offset indent -compact
56.It Fl c Ar string
57Read commands from
58.Ar string .
59.It Fl s
60Read commands from standard input
61(the default).
62.It Ar file
63Read commands from
64.Ar file .
65.El
66.Pp
67The options below can be specified with a
68.Sq Cm +
69rather than
70.Sq Fl ,
71meaning to unset the option.
72They can also be set or unset using the
73.Ic set
74command.
75Some options have equivalent long names,
76indicated at the start of the description,
77which can be used with the
78.Fl o
79option.
80.Bl -tag -width Ds
81.It Fl a
82Allexport.
83Variable assignments are exported to all child processes
84of the running shell.
85If the assignment precedes a command it does not persist
86after that command has finished running,
87unless the command is a special builtin
88or one of the builtins
89.Ic getopts
90or
91.Ic read
92makes the assignment.
93.It Fl b
94Notify.
95The user is given notice asynchronously when background jobs complete.
96.It Fl C
97Noclobber.
98Do not permit the redirection operator
99.Pq Sq >
100to clobber (overwrite) existing files.
101.It Fl e
102Errexit.
103Exit the shell immediately should an error occur or a command fail.
104For pipelines and
105.Cm &&
106and
107.Cm ||
108constructs, only exit if the last component fails.
109Errexit is ignored for
110.Ic while ,
111.Ic until ,
112.Ic if ,
113and
114.Ic elif
115lists and pipelines beginning
116.Sq !\& .
117.It Fl f
118Noglob.
119Do not expand file name patterns.
120.It Fl h
121When a utility is first executed,
122hash (record) its location
123so that future invocations do not need to search for it.
124.It Fl i
125Enable behaviour convenient for an interactive shell.
126This option is set by default
127if the session is attached to a terminal.
128.It Fl m
129Monitor.
130Fully enable job control:
131enable the
132.Ic bg
133and
134.Ic fg
135builtins;
136report completion status when jobs finish;
137report when a foreground process stops;
138and report when a job changes status.
139The processes of a job share their own process group.
140This option is set by default for interactive shells.
141.It Fl n
142Noexec.
143Read commands but do not execute them \(en
144useful for checking syntax errors in scripts.
145This option is ignored for interactive shells.
146.It Fl o Ar option
147Specify an option by its long name.
148Those described below have no equivalent option letter:
149.Pp
150.Bl -tag -width "ignoreeof" -offset 3n -compact
151.It ignoreeof
152Ignore an end-of-file
153.Pq Sq ^D .
154EOF normally logs a user out,
155so setting this can prevent accidental logouts
156(the user will need to explicitly use the
157.Ic exit
158command).
159.It nolog
160Do not enter function definitions into command history.
161.It posix
162Enable POSIX mode
163(see
164.Sx STANDARDS ) .
165.It vi
166Enable
167.Xr vi 1
168command line editing.
169.El
170.It Fl u
171Nounset.
172If a command references an unset parameter,
173write an error to standard output instead of executing the command.
174This option is ignored for the special parameters
175.Sq *
176and
177.Sq @ .
178If the shell is not interactive,
179immediately exit.
180.It Fl v
181Verbose.
182Write input to standard error after reading it.
183.It Fl x
184Xtrace.
185Write a trace for each command to standard error after expanding it,
186and before executing it.
187.El
188.Sh BUILTINS
189The shell has a number of
190.Em built-ins
191available:
192utilities that are included as part of the shell.
193The shell does not need to search for them
194and can execute them directly.
195.Pp
196A number of built-ins are special in that
197a syntax error can cause a running shell to abort,
198and, after the built-in completes,
199variable assignments remain in the current environment.
200The following built-ins are special:
201.Ic .\& , :\& , break , continue ,
202.Ic eval , exec , exit , export ,
203.Ic readonly , return , set , shift ,
204.Ic times , trap ,
205and
206.Ic unset .
207.Pp
208The built-ins available to
209.Nm
210are listed below.
211Unless otherwise indicated,
212they exit 0 on success,
213and >0 if an error occurs.
214.Bl -tag -width 2n
215.It Ic .\& Ar file
216Execute the commands in
217.Ar file ,
218in the current environment.
219The actual file need not be executable,
220and its location is determined by searching
221.Ev PATH
222if there are no slashes in the filename.
223The exit status is that of the last command returned,
224or zero if no commands were executed.
225If no readable file can be found,
226a non-interactive shell will abort;
227an interactive shell writes an error message
228and returns a non-zero exit status.
229.It Ic :\& Op Ar arg ...
230The
231.Ic :\&
232command does nothing \(en
233it is a placeholder for when a command is required.
234Its exit status is always zero.
235.It Ic alias Op Ar name Ns Oo = Ns Ar value Oc Ar ...
236Define an alias
237.Ar name
238to
239.Ar value ;
240when the shell encounters a command name that is an alias,
241its value is substituted.
242If
243.Ar value
244ends in a blank,
245the next word is checked for alias substitution too.
246If only a
247.Ar name
248is specified,
249display the value of that alias;
250if no arguments are given,
251list all aliases and their values.
252Aliases are visible in the current environment and that of subshells,
253but not by the parent process of the current shell
254or by utilities invoked by it.
255.It Ic bg Op Ar id ...
256Select a job by
257.Ar id
258(see the
259.Ic jobs
260command, below)
261to run in the background.
262The default job is
263.Qq %+ .
264.It Ic break Op Ar n
265Exit from the innermost
266.Ic for , while ,
267or
268.Ic until
269loop,
270or from loop level
271.Ar n .
272.It Ic cd Oo Fl L | P Oc Op Ar dir
273Change the current working directory to
274.Ar dir ,
275or
276.Ev $HOME
277by default.
278If
279.Ar dir
280is set to
281.Sq - ,
282change to the previous working directory and
283print the (now current) working directory.
284If
285.Ar dir
286does not begin with a slash or dot,
287.Ev CDPATH
288is searched for the directory.
289.Pp
290The options to the
291.Ic cd
292command are as follows:
293.Pp
294.Bl -tag -width Ds -offset 3n -compact
295.It Fl L
296Do not resolve symbolic links before processing
297.Qq ..
298components.
299.It Fl P
300Resolve symbolic links before processing
301.Qq ..
302components.
303.El
304.It Ic command Oo Fl p | V | v Oc Ar command Op Ar arg ...
305Invoke
306.Ar command
307(and any optional arguments),
308overriding any functions with the same name,
309and without any of the properties that special built-ins have.
310.Pp
311The options to
312.Ic command
313are as follows:
314.Pp
315.Bl -tag -width Ds -offset 3n -compact
316.It Fl p
317Use a default value for
318.Ev PATH
319to search for the command.
320.It Fl V
321Do not invoke
322.Ar command ,
323but identify how the shell will interpret it
324(such as a function or special built-in).
325.It Fl v
326Do not invoke
327.Ar command ,
328but identify the pathname the shell will use to run it.
329For aliases, a command to define that alias is printed.
330For shell reserved words, shell functions, and built-in utilities,
331just the name is printed.
332.El
333.Pp
334The exit status is that of
335.Ar command ,
336or 126 if
337.Ar command
338could not be invoked,
339or 127 if an error occurred in
340.Ic command
341itself or
342.Ar command
343could not be found.
344If the options
345.Fl V
346or
347.Fl v
348are given,
349the exit status is 0 on success,
350or >0 if an error occurs.
351.It Ic continue Op Ar n
352Go directly to the next iteration of the innermost
353.Ic for , while ,
354or
355.Ic until
356loop,
357or from loop level
358.Ar n .
359.It Ic eval Op Ar arg ...
360Concatenate the arguments given
361and interpret them as a command.
362The exit status is that of the resulting command,
363zero if no arguments are given,
364or >0 if the resulting command could not be correctly parsed.
365.It Ic exec Op Ar command Op Ar arg ...
366Replace the shell with
367.Ar command
368(and any optional arguments),
369without creating a new process.
370The exit status is that of
371.Ar command ,
372or 126 if
373.Ar command
374could not be invoked,
375or 127 if
376.Ar command
377could not be found.
378If no command is given but a redirection happens,
379the exit status is 1\(en125;
380otherwise
381.Ic exec
382returns 0.
383.It Ic exit Op Ar n
384Exit the shell with exit status
385.Ar n ,
386or that of the last command executed.
387.It Ic export Oo Fl p Oc Ar name Ns Oo = Ns Ar value Oc Ar ...
388Make the variable
389.Ar name
390visible to subsequently run commands,
391optionally setting it to
392.Ar value .
393.Pp
394The options to the
395.Ic export
396command are as follows:
397.Pp
398.Bl -tag -width Ds -offset 3n -compact
399.It Fl p
400List all exported variables in a manner that can be reinput to the shell.
401.El
402.It Ic false
403Return a false (non-zero) value.
404.It Xo
405.Ic fc
406.Op Fl lnr
407.Op Fl e Ar editor
408.Op Fl s Op Ar old Ns = Ns Ar new
409.Op Ar first Op Ar last
410.Xc
411Edit commands from command history using
412.Xr ed 1 .
413After editing,
414the new commands are executed by the shell.
415.Pp
416The options to the
417.Ic fc
418command are as follows:
419.Pp
420.Bl -tag -width "-s [old=new]" -offset 3n -compact
421.It Fl e Ar editor
422Edit commands using
423.Ar editor .
424See also
425.Ev FCEDIT .
426.It Fl l
427List the command history.
428.It Fl ln
429List the command history without command numbers.
430.It Fl r
431Edit or list
432.Pq Fl lr
433commands in reverse order.
434.It Fl s Op Ar old Ns = Ns Ar new
435Reexecute a single command
436without invoking an editor.
437The first occurrence of the string
438.Ar old
439in the command is replaced by
440.Ar new .
441.El
442.Pp
443A range of commands can be specified,
444.Ar first
445to
446.Ar last .
447Their format can be numerical,
448to select by command number;
449.Sq - Ns Ar n ,
450to select a command executed that number of commands previous;
451or a string which matches the beginning of the command.
452If no range is given,
453the last command in command history is edited,
454or reexecuted
455.Pq Fl s ,
456or the previous 16 commands in command history are listed
457.Pq Fl l .
458If
459.Ar first
460is newer than
461.Ar last ,
462commands are processed in reverse order
463(as if
464.Fl r
465had been given);
466if either are out of range,
467the oldest or newest values are used.
468.It Ic fg Op Ar id ...
469Select a job by
470.Ar id
471(see the
472.Ic jobs
473command, below)
474to run in the foreground.
475The default job is
476.Qq %+ .
477.It Ic getopts Ar optstring name Op Ar arg ...
478When invoked,
479.Ic getopts
480processes the positional parameters
481(or any
482.Ar arg
483passed to it)
484as a list of options and option arguments.
485.Ic getopts
486sets the variable
487.Ar name
488to the option found,
489.Ev OPTARG
490to its argument,
491and
492.Ev OPTIND
493to the index of the next variable to be processed.
494.Pp
495The string
496.Ar optstring
497contains a list of acceptable options;
498a colon following an option indicates it requires an argument.
499If an option not recognised by
500.Ar optstring
501is found,
502.Ar name
503is set to
504.Sq ?\& ;
505if the first character of
506.Ar optstring
507is a colon,
508.Ev OPTARG
509is set to the unsupported option,
510otherwise an error message is displayed.
511.Pp
512The following code fragment shows how one might process the arguments
513for a command that can take the option
514.Fl a
515and the option
516.Fl o ,
517which requires an argument.
518.Bd -literal -offset indent
519while getopts ao: name
520do
521	case $name in
522	a)	flag=1 ;;
523	o)	oarg=$OPTARG ;;
524	?)	echo "Usage: ..."; exit 2 ;;
525	esac
526done
527shift $(($OPTIND - 1))
528echo "Non-option arguments: " "$@"
529.Ed
530.It Ic hash Op Fl r | Ar utility
531Add
532.Ar utility
533to the hash list
534or remove
535.Pq Fl r
536all utilities from the hash list.
537Without arguments, show the utilities currently hashed.
538.It Ic jobs Oo Fl l | p Oc Op Ar id ...
539Display the status of all jobs in the current shell environment,
540or those selected by
541.Ar id .
542.Pp
543The options to the
544.Ic jobs
545command are as follows:
546.Pp
547.Bl -tag -width Ds -offset 3n -compact
548.It Fl l
549Additionally display the process group ID.
550.It Fl p
551Display only the process group ID.
552.El
553.Pp
554Job
555.Ar id
556can be selected in one of the following ways:
557.Pp
558.Bl -tag -width "%?string" -offset 3n -compact
559.It %%
560The current job.
561.It %+
562The current job.
563.It %-
564The previous job.
565.It % Ns Ar n
566Job number
567.Ar n .
568.It % Ns Ar string
569Job with command matching
570.Ar string .
571.It %? Ns Ar string
572Job with command containing
573.Ar string .
574.El
575.It Xo
576.Ic kill
577.Op Fl l Op Ar signal
578.Op Fl s Ar signal
579.Oo Fl Ar signal Oc Ar pid ...
580.Xc
581Send a signal,
582by default
583.Dv SIGTERM ,
584to the process with ID
585.Ar pid .
586.Pp
587The options to the
588.Ic kill
589command are as follows:
590.Pp
591.Bl -tag -width "-l [signal]" -offset 3n -compact
592.It Fl l Op Ar signal
593List all supported signals,
594or the signal name corresponding to
595.Ar signal
596number or the exit status of a command killed by a signal.
597.It Fl s Ar signal
598Send the process
599.Ar signal
600name.
601.It Fl Ar signal
602Send the process
603.Ar signal
604name or number.
605.It Ar pid
606A process ID,
607process group ID,
608or a job ID (see
609.Ic jobs ,
610above).
611The process ID 0 signals all processes in the current process group.
612.El
613.Pp
614The supported signal numbers are:
615.Pp
616.Bl -tag -width Ds -offset 3n -compact
617.It " 0"
618Do not signal a process,
619but determine whether an ID exists.
620.It " 1"
621.Dv SIGHUP :
622Terminal line hangup.
623.It " 2"
624.Dv SIGINT :
625Interrupt a program.
626.It " 3"
627.Dv SIGQUIT :
628Quit a program.
629.It " 6"
630.Dv SIGABRT :
631Call
632.Xr abort 3 .
633.It " 9"
634.Dv SIGKILL :
635Kill a program.
636Cannot be caught or ignored.
637.It "14"
638.Dv SIGALRM :
639Real-time timer expired.
640.It "15"
641.Dv SIGTERM :
642Software termination signal.
643.El
644.It Ic pwd Op Fl L | P
645Print the current working directory.
646.Pp
647The options to the
648.Ic pwd
649command are as follows:
650.Pp
651.Bl -tag -width Ds -offset 3n -compact
652.It Fl L
653Print the logical path to the current working directory
654i.e. display symbolic links followed.
655.It Fl P
656Print the physical path to the current working directory
657i.e. display symbolic links resolved.
658.El
659.Pp
660If both options are given,
661the last specified is used;
662if none are given,
663the default is
664.Fl L .
665.It Ic read Oo Fl r Oc Ar name ...
666Read a line from standard input.
667The line is split into fields,
668with each field assigned to a variable,
669.Ar name ,
670in turn
671(first field assigned to first variable, and so on).
672If there are more fields than variables,
673the last variable will contain all the remaining fields.
674If there are more variables than fields,
675the remaining variables are set to empty strings.
676A backslash in the input line causes the shell to prompt for further input.
677.Pp
678The options to the
679.Ic read
680command are as follows:
681.Pp
682.Bl -tag -width Ds -offset 3n -compact
683.It Fl r
684Ignore backslash sequences.
685.El
686.It Ic readonly Oo Fl p Oc Ar name Ns Op = Ns Ar value
687Mark variable
688.Ar name
689as readonly,
690and optionally set it to
691.Ar value .
692Readonly variables cannot be later assigned values or unset.
693.Pp
694The options to the
695.Ic readonly
696command are as follows:
697.Pp
698.Bl -tag -width Ds -offset 3n -compact
699.It Fl p
700Display the names and values of all readonly variables
701in a manner which can be reinput to the shell.
702.El
703.It Ic return Op Ar n
704Exit the current function or
705.Ic .\&
706script with exit status
707.Ar n ,
708or that of the last command executed.
709.It Xo
710.Ic set
711.Op Fl abCefhmnuvx
712.Op Fl o Op Ar option
713.Op Ar arg ...
714.Xc
715Set options and positional parameters.
716Without options or arguments,
717display the names and values of all shell variables.
718.Pp
719The options are described in the options description
720at the beginning of this manual.
721The sequence
722.Ql set -o
723displays the current option settings;
724the sequence
725.Ql set +o
726displays,
727in a format suitable to be reinput to the shell,
728a command suitable to achieve the current option settings.
729.Pp
730Any arguments are assigned to the positional parameters,
731with the special parameter
732.Sq #
733set to the number of positional parameters.
734The sequence
735.Ql set --
736indicates an end to option processing
737(i.e. only arguments follow);
738.Ql set --
739by itself unsets all positional parameters
740and sets
741.Sq #
742to zero.
743.It Ic shift Op Ar n
744Shift the positional parameters
745.Ar n
746times
747(by default once).
748Parameter 1 takes the value of parameter
749.Sq 1+ Ns Ar n ,
750parameter 2 takes
751.Sq 2+ Ns Ar n ,
752and so on.
753Parameters
754.Sq #
755down to
756.Sq Po #\(mi Ns Ar n Pc Ns +1
757are unset and
758.Sq #
759is updated to the new number of positional parameters.
760If
761.Ar n
762is 0,
763no change occurs.
764.It Ic times
765Display accumulated process times for the shell (user and system)
766and all child processes (user and system).
767.It Ic trap Op Ar action signal ...
768Perform
769.Ar action
770whenever
771.Ar signal
772is caught.
773Without arguments,
774display a list of all traps and actions,
775in a format suitable to be reinput to the shell.
776.Pp
777If
778.Ar action
779is
780.Sq -
781or an integer,
782reset
783.Ar signal
784to its default value;
785if it is empty
786.Pq Qq ,
787ignore
788.Ar signal .
789If
790.Ar signal
791is
792.Qq EXIT
793or 0,
794perform
795.Ar action
796when the shell exits;
797otherwise
798.Ar signal
799should be a signal name
800(without the SIG prefix)
801or number.
802.It Ic true
803Return a true (zero) value.
804.It Ic type Ar command ...
805For each
806.Ar command ,
807show how the shell would interpret it.
808.It Ic ulimit Op Fl f Ar n
809Limit the maximum size of a file that can be created to
810.Ar n
811blocks.
812Without arguments,
813display the current file size limit.
814.It Ic umask Oo Fl S Oc Op Ar mask
815Set the file mode creation mask to
816.Ar mask .
817The creation mask determines the default permissions
818a newly created file or directory will have.
819If
820.Ar mask
821is not specified,
822display the current creation mask.
823.Pp
824The options to the
825.Ic umask
826command are as follows:
827.Pp
828.Bl -tag -width Ds -offset 3n -compact
829.It Fl S
830Display symbolic output.
831.El
832.Pp
833See
834.Xr chmod 1
835for the format of
836.Ar mask .
837.It Ic unalias Oo Fl a Oc Ar name ...
838Remove the alias definition of alias
839.Ar name .
840.Pp
841The options to the
842.Ic unalias
843command are as follows:
844.Pp
845.Bl -tag -width Ds -offset 3n -compact
846.It Fl a
847Remove all alias definitions.
848.El
849.It Ic unset Oo Fl fv Oc Ar name ...
850Unset variable or function
851.Ar name .
852.Pp
853The options to the
854.Ic unset
855command are as follows:
856.Pp
857.Bl -tag -width Ds -offset 3n -compact
858.It Fl f
859Treat
860.Ar name
861as a function.
862.It Fl v
863Treat
864.Ar name
865as a variable (the default).
866.El
867.It Ic wait Op Ar pid ...
868Wait until all the processes specified by process or job ID
869.Ar pid
870have terminated.
871If no
872.Ar pid
873is specified,
874wait until all processes have terminated.
875The exit status is 0 on success,
8761\(en126 if an error occurs,
877or 127 if
878.Ar pid
879was unknown.
880.El
881.Sh COMMAND HISTORY AND COMMAND LINE EDITING
882When a shell is interactive,
883it keeps a record of commands run in a
884.Em command history ,
885either internally in memory or in a file,
886as determined by
887.Dv HISTFILE .
888When
889.Cm vi
890command line editing mode is enabled
891.Pq set -o vi ,
892the command line and all the commands in command history
893can be edited using commands similar to those of
894.Xr vi 1 .
895.Pp
896There are two modes,
897.Em interactive
898and
899.Em command .
900The shell starts in interactive mode.
901In this mode text is entered normally.
902A
903.Aq newline
904executes the current command line.
905The command line, unless empty, is entered into command history.
906The
907.Aq ESC
908key is used to enter command mode,
909where commands similar to those used by
910.Xr vi 1
911are available.
912A Ctrl-L sequence
913.Pq ^L
914can be used in this mode to
915redraw the current command line.
916.Pp
917Where noted,
918some commands may be preceded by a numerical
919.Ar count ,
920which causes the command to be repeated that number of times.
921The term
922.Em word
923is used to denote a sequence of letters, digits, or underscores;
924.Em bigword
925denotes a sequence of whitespace delineated characters.
926.Pp
927The commands for command mode:
928.Bl -tag -width "<newline>"
929.It Ic =
930Display the possible shell word expansion.
931.It Ic \e
932Perform pathname expansion on the current word,
933matching the largest possible unique expansion,
934then enter insert mode.
935.It Ic *
936Perform pathname expansion on the current word,
937substituting every possible expansion,
938then enter insert mode.
939.It Ic @ Ns Ar c
940Perform the commands defined by the alias
941.No _ Ns Ar c ,
942where
943.Ar c
944is a single letter alphabetical character.
945.It Oo Ar count Oc Ns Ic ~
946Convert the character from lowercase to upper or vice versa.
947.It Oo Ar count Oc Ns Ic .\&
948Repeat the most recent non-motion command.
949If no
950.Ar count
951is given, use that of the repeated command,
952if any.
953.It Oo Ar n Oc Ns Ic v
954Use
955.Xr vi 1
956to edit command number
957.Ar n
958in command history,
959or the current command if none given.
960.It Xo
961.Oo Ar count Oc Ns Ic l ,
962.Oo Ar count Oc Ns Aq space
963.Xc
964Move right.
965.It Oo Ar count Oc Ns Ic h
966Move left.
967.It Oo Ar count Oc Ns Ic w
968Move to the start of the next word.
969.It Oo Ar count Oc Ns Ic W
970Move to the start of the next big word.
971.It Oo Ar count Oc Ns Ic e
972Move to the end of the current word,
973or the end of the next word if the cursor is currently
974at the end of a word.
975.It Oo Ar count Oc Ns Ic E
976Move to the end of the current bigword,
977or the end of the next bigword if the cursor is currently
978at the end of a bigword.
979.It Oo Ar count Oc Ns Ic b
980Move to the start of the current word,
981or the start of the next word if the cursor is currently
982at the start of a word.
983.It Oo Ar count Oc Ns Ic B
984Move to the start of the current bigword,
985or the start of the next bigword if the cursor is currently
986at the start of a bigword.
987.It Ic ^
988Move to the first non-blank character.
989.It Ic $
990Move to the end of the current line.
991.It Ic 0
992Move to the beginning of the current line.
993.It Oo Ar count Oc Ns Ic |\&
994Move to the beginning of the current line
995or the character position specified by
996.Ar count .
997.It Oo Ar count Oc Ns Ic f Ns Ar c
998Move to the next instance of the
999character
1000.Ar c .
1001.It Oo Ar count Oc Ns Ic F Ns Ar c
1002Move to the last instance of the
1003character
1004.Ar c .
1005.It Oo Ar count Oc Ns Ic t Ns Ar c
1006Move to the character before the next instance of the
1007character
1008.Ar c .
1009.It Oo Ar count Oc Ns Ic T Ns Ar c
1010Move to the character after the last instance of the
1011character
1012.Ar c .
1013.It Oo Ar count Oc Ns Ic ;\&
1014Repeat the last
1015.Ic f , F , t ,
1016or
1017.Ic T
1018command.
1019Ignore any
1020.Ar count
1021specified with the last command.
1022.It Oo Ar count Oc Ns Ic ,\&
1023Repeat the last
1024.Ic f , F , t ,
1025or
1026.Ic T
1027command,
1028but in the opposite direction.
1029Ignore any
1030.Ar count
1031specified with the last command.
1032.It Ic a
1033Enter insert mode after the current cursor position.
1034.It Ic A
1035Enter insert mode after the end of the current line.
1036.It Ic i
1037Enter insert mode at the current cursor position.
1038.It Ic I
1039Enter insert mode at the beginning of the current line.
1040.It Ic R
1041Enter insert mode at the current cursor position,
1042replacing any characters thereafter.
1043.It Oo Ar count Oc Ns Ic c Ns Ar motion
1044Delete the characters between the cursor and the motion command specified,
1045then enter insert mode.
1046A special motion command,
1047.Ic c ,
1048may be used to delete the entire line.
1049The
1050.Ar count
1051argument is ignored for the commands
1052.Ic 0 , ^ , $ ,
1053and
1054.Ic c .
1055If the motion moves towards the beginning of the line,
1056the character under the cursor is not deleted;
1057if it moves towards the end of the line,
1058it is deleted.
1059.It Ic C
1060Delete the characters between the cursor and the line end,
1061then enter insert mode.
1062.It Ic S
1063Clear the entire line,
1064then enter insert mode.
1065.It Oo Ar count Oc Ns Ic r
1066Replace the character under the cursor with the next typed character.
1067With a
1068.Ar count ,
1069replace the current character
1070and the corresponding number of following characters.
1071.It Oo Ar count Oc Ns Ic _
1072After the cursor,
1073append a
1074.Aq space
1075and the
1076.Ar count Ns th
1077bigword (by default the last entered)
1078from the previous input line,
1079then enter insert mode.
1080.It Oo Ar count Oc Ns Ic x
1081Delete the character under the cursor,
1082placing it in the save buffer.
1083.It Oo Ar count Oc Ns Ic X
1084Delete the character before the cursor,
1085placing it in the save buffer.
1086.It Oo Ar count Oc Ns Ic d Ns Ar motion
1087Delete the characters between the cursor and the motion command specified,
1088placing them in the save buffer.
1089A special motion command,
1090.Ic d ,
1091may be used to delete the entire line.
1092If the motion moves towards the beginning of the line,
1093the character under the cursor is not deleted.
1094.It Oo Ar count Oc Ns Ic D
1095Delete the characters between the cursor and the line end,
1096placing them in the save buffer.
1097.It Oo Ar count Oc Ns Ic y Ns Ar motion
1098Yank (copy) the characters between the cursor and the motion command specified,
1099placing them in the save buffer.
1100A special motion command,
1101.Ic y ,
1102may be used to yank the entire line.
1103If the motion moves towards the beginning of the line,
1104the character under the cursor is not yanked.
1105.It Oo Ar count Oc Ns Ic Y
1106Yank (copy) the characters between the cursor and the line end,
1107placing them in the save buffer.
1108.It Oo Ar count Oc Ns Ic p
1109Paste the contents of the save buffer after the cursor.
1110.It Oo Ar count Oc Ns Ic P
1111Paste the contents of the save buffer before the cursor.
1112.It Oo Ar count Oc Ns Ic u
1113Undo the last change to the edit line.
1114.It Oo Ar count Oc Ns Ic U
1115Undo all changes to the edit line.
1116.It Xo
1117.Oo Ar count Oc Ns Ic k ,
1118.Oo Ar count Oc Ns Ic -\&
1119.Xc
1120Replace the current command line with the previous entry in history.
1121.It Xo
1122.Oo Ar count Oc Ns Ic j ,
1123.Oo Ar count Oc Ns Ic +\&
1124.Xc
1125Replace the current command line with the next entry in history.
1126.It Oo Ar n Oc Ns Ic G
1127Replace the current command line with command number
1128.Ar n
1129in command history,
1130or the oldest command if none given.
1131.It / Ns Ar pattern
1132Moving backwards through history,
1133replace the current command line with the first that matches
1134.Ar pattern .
1135A
1136.Sq ^
1137at the beginning of the pattern searches only for entries beginning with
1138.Ar pattern .
1139An empty pattern matches the last search.
1140.It ? Ns Ar pattern
1141As above,
1142but searching forwards.
1143.It Ic n
1144Repeat the most recent pattern search.
1145.It Ic N
1146Repeat the most recent pattern search,
1147but in the opposite direction.
1148.El
1149.Sh SHELL GRAMMAR
1150The shell reads its input as described above.
1151After that it follows a fairly simple chain of operations
1152to parse that input:
1153.Bl -dash
1154.It
1155The shell breaks the input into
1156.Em words
1157and
1158.Em operators .
1159Words are the command text the user wishes run;
1160operators are special characters which describe
1161how the shell should interact with the commands.
1162.It
1163The shell
1164.Em expands
1165the command text according to the rules of expansion.
1166.It
1167Words are subject to
1168.Em field splitting ,
1169where the command text is separated into commands
1170and arguments to commands.
1171.It
1172The shell performs any
1173.Em redirection .
1174.It
1175The shell runs the commands.
1176Argument names are assigned to
1177.Em positional parameters ,
1178with the command name itself assigned parameter 0.
1179.It
1180If the command is not being run in the background,
1181the shell waits for it to complete
1182and collects its exit status.
1183.El
1184.Ss Quoting
1185Some characters have special meaning to the shell and need
1186.Em quoting
1187if the user wants to indicate to the shell not to interpret them as such.
1188The following characters need quoting if their literal meaning is desired:
1189.Bd -literal -offset indent
1190| & ; < > ( ) $ \` \e " \(aq <space> <tab> <newline>
1191* ? [ # ~ = %
1192.Ed
1193.Pp
1194A backslash
1195.Pq \e
1196can be used to quote any character except a newline.
1197If a newline follows a backslash the shell removes them both,
1198effectively making the following line part of the current one.
1199.Pp
1200A group of characters can be enclosed within single quotes
1201.Pq \(aq
1202to quote every character within the quotes.
1203.Pp
1204A group of characters can be enclosed within double quotes
1205.Pq \&"
1206to quote every character within the quotes
1207except a backquote
1208.Pq \`
1209or a dollar sign
1210.Pq $ ,
1211both of which retain their special meaning.
1212A backslash
1213.Pq \e
1214within double quotes retains its special meaning,
1215but only when followed by a backquote, dollar sign,
1216double quote, newline, or another backslash.
1217An at sign
1218.Pq @
1219within double quotes has a special meaning
1220(see
1221.Sx SPECIAL PARAMETERS ,
1222below).
1223.Pp
1224Similarly command words need to be quoted
1225if they are not to be interpreted as such.
1226.Ss Expansion
1227Shell
1228.Em variables
1229are arbitrary names assigned values using the
1230.Sq =
1231operator;
1232the values can be retrieved using the syntax
1233.No $ Ns Ar variable .
1234Shell
1235.Em parameters
1236are variable names,
1237numbers,
1238or any of the characters listed in
1239.Sx SPECIAL PARAMETERS .
1240.Pp
1241The shell is able to
1242.Em expand
1243certain elements of its syntax,
1244allowing for a more concise notation
1245and providing a convenience to the user.
1246.Pp
1247Firstly, tilde expansion occurs on words beginning with the
1248.Sq ~
1249character.
1250Any characters following the tilde,
1251up to the next colon, slash, or blank,
1252are taken as a login name
1253and substituted with that user's home directory,
1254as defined in
1255.Xr passwd 5 .
1256A tilde by itself is expanded to the contents of the variable
1257.Ev HOME .
1258This notation can be used in variable assignments,
1259in the assignment half,
1260immediately after the equals sign or a colon,
1261up to the next slash or colon, if any.
1262.Pp
1263.Dl PATH=~alice:~bob/jobs
1264.Pp
1265Parameter expansion happens after tildes have been expanded,
1266with the value of the parameter being substituted.
1267The basic format is:
1268.Pp
1269.D1 $ Ns Brq Ar parameter
1270.Pp
1271The braces are optional
1272except for positional parameters 10 and higher,
1273or where the parameter name is followed by other characters
1274that would prevent it from being expanded.
1275If parameter expansion occurs within double quotes,
1276neither pathname expansion nor field splitting happens afterwards.
1277.Pp
1278Some special forms of parameter expansion are available.
1279In the formats below,
1280.Ar word
1281itself is subject to expansion,
1282and, if omitted,
1283the empty string is used.
1284If the colon is omitted,
1285.Ar word
1286is substituted only if
1287.Ar parameter
1288is unset (not if it is empty).
1289.Bl -tag -width Ds
1290.It $ Ns Brq Ar parameter Ns :- Ns Op Ar word
1291Substitute
1292.Ar parameter .
1293If
1294.Ar parameter
1295is unset or empty,
1296substitute
1297.Ar word .
1298.It $ Ns Brq Ar parameter Ns := Ns Op Ar word
1299Substitute
1300.Ar parameter .
1301If
1302.Ar parameter
1303is unset or empty,
1304first assign the value of
1305.Ar word
1306to
1307.Ar parameter .
1308.It $ Ns Brq Ar parameter Ns :? Ns Op Ar word
1309Substitute
1310.Ar parameter .
1311If
1312.Ar parameter
1313is unset or empty,
1314the result of the expansion of
1315.Ar word
1316is written to standard error
1317and the shell exits with a non-zero exit status.
1318If
1319.Ar word
1320is omitted,
1321the string
1322.Qq parameter null or not set
1323is used.
1324.It $ Ns Brq Ar parameter Ns :+ Ns Op Ar word
1325Substitute
1326.Ar word .
1327If
1328.Ar parameter
1329is unset or empty,
1330substitute the empty string.
1331.It $ Ns Brq # Ns Ar parameter
1332The length, in characters, of
1333.Ar parameter .
1334.It $ Ns Brq Ar parameter Ns % Ns Op Ar word
1335Substitute
1336.Ar parameter ,
1337deleting the smallest possible suffix matching
1338.Ar word .
1339.It $ Ns Brq Ar parameter Ns %% Ns Op Ar word
1340Substitute
1341.Ar parameter ,
1342deleting the largest possible suffix matching
1343.Ar word .
1344.It $ Ns Brq Ar parameter Ns # Ns Op Ar word
1345Substitute
1346.Ar parameter ,
1347deleting the smallest possible prefix matching
1348.Ar word .
1349.It $ Ns Brq Ar parameter Ns ## Ns Op Ar word
1350Substitute
1351.Ar parameter ,
1352deleting the largest possible prefix matching
1353.Ar word .
1354.El
1355.Pp
1356Command expansion has a command executed in a subshell
1357and the results output in its place.
1358The basic format is:
1359.Pp
1360.D1 $ Ns Pq Ar command
1361or
1362.D1 \` Ns Ar command Ns \`
1363.Pp
1364The results are subject to field splitting and pathname expansion;
1365no other form of expansion happens.
1366If
1367.Ar command
1368is contained within double quotes,
1369field splitting does not happen either.
1370Within backquotes,
1371a backslash is treated literally unless it follows
1372a dollar sign, backquote, or another backslash.
1373Commands can be nested,
1374though the backquoted version requires backslashes before the backquotes.
1375If
1376.Ar command
1377is run in a subshell in the bracketed version,
1378the syntax is identical to that of arithmetic expansion.
1379In that case the shell attempts arithmetic expansion first,
1380then attempts command substitution if that fails.
1381Or a non-ambiguous version can be used:
1382.Pp
1383.D1 "$( (" Ns Ar command Ns ") )"
1384.Pp
1385Arithmetic expansion works similarly,
1386with an arithmetic expression being evaluated and substituted.
1387The format is:
1388.Pp
1389.D1 $ Ns Pq Pq Ar expression
1390.Pp
1391Where
1392.Ar expression
1393is an integer or parameter name,
1394optionally combined with any of the operators described below,
1395listed and grouped according to precedence:
1396.Bl -tag -width Ds
1397.It ()\&
1398Operators within brackets have highest precedence.
1399Compare 3+2*4, which is 11,
1400since multiplication has higher precedence than addition,
1401and (3+2)*4, which is 20.
1402.It + - ~ !\&
1403Unary plus
1404(indicates a positive value; integers are positive by default),
1405unary minus (indicates a negative value),
1406bitwise NOT,
1407and logical NOT
1408(the result is 1 if the argument is zero, or 0 otherwise), respectively.
1409.It * / %
1410Multiplication, division, and modulus (remainder), respectively.
1411.It + -
1412Addition and subtraction, respectively.
1413.It << >>
1414Shift left or right, respectively.
1415.It < <= > >=
1416Less than, less than or equal to,
1417greater than, and greater than or equal to, respectively.
1418The result is 1 if true, or 0 otherwise.
1419.It == !=
1420Equal (the result is 1 if both arguments are equal, and 0 otherwise)
1421and not equal (the result is 0 if both arguments are equal, and 1 otherwise),
1422respectively.
1423.It &
1424Bitwise AND.
1425.It ^
1426Bitwise exclusive OR.
1427.It |
1428Bitwise inclusive OR.
1429.It &&
1430Logical AND.
1431The result is 1 if both arguments are non-zero, or 0 otherwise.
1432.It ||
1433Logical OR.
1434The result is 1 if either argument is non-zero, or 0 otherwise.
1435.It Ar expression ? Ns Ar expr1 : Ns Ar expr2
1436The result is
1437.Ar expr1
1438if
1439.Ar expression
1440is non-zero,
1441or
1442.Ar expr2
1443otherwise.
1444.It = *= /= %= += -= <<= >>= &= ^= |=
1445Assignment.
1446The notation
1447.Ar var Ns *= Ns Ar expression
1448is equivalent to
1449.Ar var Ns = Ns Ar var Ns * Ns Ar expression .
1450.El
1451.Pp
1452After the various types of expansion listed above have been carried out,
1453the shell subjects everything that did not occur in double quotes to
1454.Em field splitting ,
1455where words are broken up according to the value of the
1456.Ev IFS
1457variable.
1458Each character of
1459.Ev IFS
1460is used to split fields;
1461any
1462.Ev IFS
1463characters at the beginning and end of input are ignored.
1464If
1465.Ev IFS
1466is unset, the default value consisting of
1467.Aq space ,
1468.Aq tab
1469and
1470.Aq newline
1471is used; if the value of
1472.Ev IFS
1473is empty, no field splitting is performed.
1474.Pp
1475After field splitting,
1476the shell matches filename patterns.
1477.Bl -tag -width Ds
1478.It ?
1479A question mark matches any single character.
1480.It *
1481An asterisk matches multiple characters.
1482.It [..]
1483Matches any character enclosed in the brackets.
1484The sense is negated if the first character is
1485.Sq !\& .
1486A closing bracket can be included in the list of characters to match
1487by listing it as the first character after the opening bracket
1488or by quoting it.
1489Similarly a
1490.Sq -
1491should be specified last or quoted so that the shell does not think
1492it is a character range (see below).
1493.It [[: Ns Ar class Ns :]]
1494Matches any character in the following character classes:
1495.Bd -literal -offset indent
1496alnum	alpha	blank	cntrl
1497digit	graph	lower	print
1498punct	space	upper	xdigit
1499.Ed
1500.It Bq Ar x Ns - Ns Ar y
1501Matches any character in the range between
1502.Ar x
1503and
1504.Ar y ,
1505inclusive.
1506.El
1507.Pp
1508Slashes and full stops do not match the patterns above
1509because of their use as path and filename characters.
1510.Ss Redirection
1511Redirection is used to open, close, or otherwise manipulate files,
1512using redirection operators in combination with numerical
1513.Em file descriptors .
1514A minimum of ten (0\-9) descriptors are supported;
1515by convention
1516standard input is file descriptor 0,
1517standard output file descriptor 1,
1518and standard error file descriptor 2.
1519In the examples given below,
1520.Ar n
1521represents a numerical file descriptor.
1522The target for redirection is
1523.Ar file
1524and it is subject to all forms of expansion as listed above,
1525except pathname expansion.
1526If any part of the file descriptor or redirection operator is quoted,
1527they are not recognised.
1528.Bl -tag -width Ds
1529.It Oo Ar n Oc Ns < Ns Ar file
1530Open
1531.Ar file
1532for reading on file descriptor
1533.Ar n ,
1534by default standard input.
1535.It Oo Ar n Oc Ns > Ns Ar file
1536Write to
1537.Ar file
1538with file descriptor
1539.Ar n ,
1540by default standard output.
1541If
1542.Ar file
1543does not exist,
1544create it;
1545if it does exist,
1546truncate it to be empty before beginning to write to it.
1547.It Oo Ar n Oc Ns >| Ns Ar file
1548As above, but forces clobbering
1549(see the
1550.Fl C
1551option).
1552.It Oo Ar n Oc Ns >> Ns Ar file
1553Append to
1554.Ar file
1555with file descriptor
1556.Ar n ,
1557by default standard output.
1558If
1559.Ar file
1560does not exist,
1561create it.
1562.It Oo Ar n Oc Ns <<
1563This form of redirection,
1564called a
1565.Em here document ,
1566is used to copy a block of lines
1567to a temporary file until a line matching
1568.Ar delimiter
1569is read.
1570When the command is executed, standard input is redirected from the
1571temporary file to file descriptor
1572.Ar n ,
1573or standard input by default.
1574The basic format is:
1575.Bd -unfilled -offset indent
1576.Oo Ar n Oc Ns << Ns Ar delimiter
1577text
1578text
1579\&...
1580.Ar delimiter
1581.Ed
1582.Pp
1583Provided
1584.Ar delimiter
1585doesn't contain any quoted characters,
1586parameter, command, and arithmetic expansions are performed on
1587the text block,
1588and backslashes escape the special meaning of
1589.Sq $ ,
1590.Sq \` ,
1591and
1592.Sq \e .
1593If multiple here documents are used on the same command line,
1594they are saved and processed in order.
1595.It Oo Ar n Oc Ns <<-
1596Same as
1597.Ic << ,
1598except leading tabs are stripped from lines in
1599.Ar block .
1600.It Oo Ar n Oc Ns <& Ns Ar file
1601Make file descriptor
1602.Ar n ,
1603by default standard input,
1604a copy of the file descriptor denoted by
1605.Ar file .
1606If
1607.Ar file
1608is
1609.Sq - ,
1610close file descriptor
1611.Ar n
1612or standard input.
1613.It Oo Ar n Oc Ns >& Ns Ar file
1614Make file descriptor
1615.Ar n ,
1616by default standard output,
1617a copy of the file descriptor denoted by
1618.Ar file .
1619If
1620.Ar file
1621is
1622.Sq - ,
1623close file descriptor
1624.Ar n
1625or standard output.
1626.It Oo Ar n Oc Ns <> Ns Ar file
1627Open
1628.Ar file
1629for reading and writing on file descriptor
1630.Ar n ,
1631by default standard input.
1632If
1633.Ar file
1634does not exist,
1635create it.
1636.El
1637.Sh COMMANDS
1638The shell first expands
1639any words that are not variable assignments or redirections,
1640with the first field being the command name
1641and any successive fields arguments to that command.
1642It sets up redirections, if any,
1643and then expands variable assignments, if any.
1644It then attempts to run the command.
1645.Pp
1646Firstly, it determines whether the command name contains any slashes.
1647If it does not, and the shell implements the command as a special built-in,
1648it then invokes the built-in.
1649If not, but it is a non POSIX standard command,
1650implemented as a shell function,
1651it then invokes that.
1652If not, but it is one of the commands
1653.Ic alias , bg , cd , command ,
1654.Ic false , fc , fg , getopts ,
1655.Ic jobs , kill , newgrp , pwd ,
1656.Ic read , true , umask , unalias ,
1657or
1658.Ic wait ,
1659it then invokes that.
1660.Pp
1661Failing that, the value of
1662.Ev PATH
1663is used to search for the command.
1664If it finds a match,
1665and it is a POSIX standard command,
1666implemented as a built-in or function,
1667it then invokes it.
1668Otherwise
1669it attempts to execute the command in an environment separate from the shell.
1670If it is unable to execute the command,
1671it tries to run it as a shell script.
1672.Pp
1673Finally, if the command name does contain a slash,
1674and it finds a match in
1675.Ev PATH ,
1676it attempts to execute the command in an environment separate from the shell.
1677If it is unable to execute the command,
1678it tries to run it as a shell script.
1679.Pp
1680A series of one or more commands separated by
1681.Sq ;\&
1682constitute a
1683.Em sequential list ,
1684where commands are executed in the order given.
1685The exit status of a sequential list is that of the last command executed.
1686The format for a sequential list is:
1687.Pp
1688.D1 Ar command No \&; Op Ar command ...
1689.Pp
1690A series of one or more commands separated by
1691.Sq &
1692constitute an
1693.Em asynchronous list ,
1694where the shell executes the command in a subshell
1695and runs the next command without waiting for the previous one to finish.
1696The exit status of an asynchronous list is always zero.
1697The format for an asynchronous list is:
1698.Pp
1699.D1 Ar command No & Op Ar command ...
1700.Pp
1701A series of commands separated by
1702.Sq |
1703constitute a
1704.Em pipeline ,
1705where the output of one command
1706is used as input for the next command.
1707The exit status of a pipeline is that of the last command;
1708if a pipeline begins
1709.Sq !\&
1710the exit status is inverted.
1711The format for a pipeline is:
1712.Pp
1713.D1 Oo !\& Oc Ar command | command Op | Ar ...
1714.Pp
1715A series of commands separated by
1716.Sq &&
1717constitute an
1718.Em AND list ,
1719where a command is only executed if the exit status of the previous command was
1720zero.
1721The exit status of an AND list is that of the last command.
1722The format for an AND list is:
1723.Pp
1724.D1 Ar command No && Ar command Op && Ar ...
1725.Pp
1726A series of commands separated by
1727.Sq ||
1728constitute an
1729.Em OR list ,
1730where a command is only executed if the exit status of the previous command was
1731non-zero.
1732The exit status of an OR list is that of the last command.
1733The format for an OR list is:
1734.Pp
1735.D1 Ar command No || Ar command Op || Ar ...
1736.Pp
1737A series of commands separated by
1738.Sq &&
1739and
1740.Sq ||
1741constitute an
1742.Em AND-OR list ,
1743where
1744.Sq &&
1745and
1746.Sq ||
1747have equal precedence and are evaluated in the order they are given.
1748The AND-OR list can be terminated with
1749.Sq ;\&
1750or
1751.Sq &
1752to have them execute sequentially or asynchronously, respectively.
1753.Pp
1754Command lists,
1755as described above,
1756can be enclosed within
1757.Sq ()
1758to have them executed in a subshell,
1759or within
1760.Sq {}
1761to have them executed in the current environment:
1762.Pp
1763.D1 Pq Ar command ...
1764.D1 Brq \& Ar command ... ; No \&
1765.Pp
1766Any redirections specified after the closing bracket apply to all commands
1767within the brackets.
1768An operator such as
1769.Sq ;\&
1770or a newline are needed to terminate a command list within curly braces.
1771.Pp
1772The shell has grammatical constructs
1773which allow it to work its way (loop) through lists
1774or evaluate things conditionally.
1775.Pp
1776A
1777.Em for loop
1778executes a series of commands for each item in a list.
1779Its format is:
1780.Bd -unfilled -offset indent
1781.No for Ar name Op in Op Ar word ...
1782do
1783.No "   " Ar command
1784.No "   " Ar ...
1785done
1786.Ed
1787.Pp
1788Firstly
1789.Ar word ...
1790is expanded to generate a list of items.
1791The variable
1792.Ar name
1793is set to each item, in turn,
1794and the commands are executed for each item.
1795The construct
1796.Qq in word ...
1797can be omitted,
1798which is equivalent to: in \&"$@\&".
1799The exit status is that of the last command executed.
1800If there are no items,
1801.Ar command
1802is not executed and the exit status is zero.
1803.Pp
1804A
1805.Em while loop
1806continuously executes a set of commands
1807as long as the command or command list being tested in
1808.Ar condition
1809has a zero exit status.
1810Its format is:
1811.Bd -unfilled -offset indent
1812.No while Ar condition
1813do
1814.No "   " Ar command
1815.No "   " Ar ...
1816done
1817.Ed
1818.Pp
1819Multiple commands may be given by grouping them in lists,
1820as described above,
1821or by separating them with newlines.
1822The exit status is zero if the commands after
1823.Qq do
1824were never executed
1825or otherwise the exit status of the last command executed.
1826.Pp
1827An
1828.Em until loop
1829continuously executes a set of commands
1830as long as the command or command list being tested in
1831.Ar condition
1832has a non-zero exit status.
1833Its format is:
1834.Bd -unfilled -offset indent
1835.No until Ar condition
1836do
1837.No "   " Ar command
1838.No "   " Ar ...
1839done
1840.Ed
1841.Pp
1842Multiple commands may be given by grouping them in lists,
1843as described above,
1844or by separating them with newlines.
1845The exit status is zero if the commands after
1846.Qq do
1847were never executed
1848or otherwise the exit status is that of the last command executed.
1849.Pp
1850A
1851.Em case conditional
1852is used to run commands whenever a pattern is matched.
1853Its format is:
1854.Bd -unfilled -offset indent
1855.No case Ar word No in
1856.No "   " Po Ar pattern Oo | Ar pattern ... Oc Pc Ar command Ns ;;
1857.No "   " Ar ...
1858esac
1859.Ed
1860.Pp
1861In this case
1862.Ar pattern
1863is matched against the string resulting from the expansion of
1864.Ar word .
1865Multiple commands may be given by grouping them in lists,
1866as described above,
1867or by separating them with newlines.
1868The initial
1869.Sq (\&
1870is optional,
1871as is the terminating
1872.Sq ;;
1873for the final command.
1874The exit status is zero if no patterns are matched
1875or otherwise the exit status of the last command executed.
1876.Pp
1877An
1878.Em if conditional
1879is used to execute commands depending on the exit status of the command or
1880command list being tested.
1881Its format is:
1882.Bd -unfilled -offset indent
1883.No if Ar conditional
1884then
1885.No "   " Ar command
1886.No "   " Ar ...
1887.No elif Ar conditional
1888then
1889.No "   " Ar command
1890.No "   " Ar ...
1891.No else
1892.No "   " Ar command
1893.No "   " Ar ...
1894fi
1895.Ed
1896.Pp
1897Firstly the command(s) following
1898.Qq if
1899is executed;
1900if its exit status is zero,
1901the commands in the
1902.Qq then
1903block are executed and the conditional completes.
1904Otherwise the commands in the
1905.Qq elif
1906block are executed;
1907if the exit status is zero,
1908the commands in the
1909.Qq then
1910block are executed and the conditional completes.
1911Otherwise the next
1912.Qq elif
1913block, if any, is tried.
1914If nothing from an
1915.Qq if
1916or
1917.Qq elif
1918block returns zero,
1919the commands in the
1920.Qq else
1921block are run and the conditional completes.
1922The
1923.Qq elif
1924and
1925.Qq else
1926blocks are optional.
1927.Pp
1928Multiple commands may be given by grouping them in lists,
1929as described above,
1930or by separating them with newlines.
1931The exit status is zero if nothing is executed from an
1932.Qq if
1933or
1934.Qq elif
1935block
1936or otherwise the exit status of the last command executed.
1937.Pp
1938Functions allow the user to define a group of commands,
1939executed whenever the function is invoked.
1940Its format is:
1941.Bd -unfilled -offset indent
1942.Ar function Ns () Ar command-list
1943.Ed
1944.Pp
1945The above simply defines a function;
1946nothing is executed until the function is invoked.
1947Commands may specify redirections
1948and positional parameters are changed,
1949for the duration of the function,
1950to those passed to it.
1951The special parameter
1952.Sq #
1953is temporarily changed too,
1954though
1955.Sq 0
1956is not.
1957After the function finishes,
1958the positional parameters and
1959.Sq #
1960are restored to their original values.
1961The exit status of a function definition is 0 if successful
1962or >0 otherwise.
1963The exit status of a function is that of the last command
1964executed by the function.
1965.Sh SPECIAL PARAMETERS
1966Some parameters have special meaning to the shell
1967and are listed below.
1968.Bl -tag -width Ds
1969.It 0
1970The name of the shell or shell script.
1971.It 1 ... n
1972The
1973.Em positional parameters .
1974These parameters are set when a shell, shell script,
1975or shell function is invoked.
1976Each argument passed to a shell or shell script
1977is assigned a positional parameter,
1978starting at 1,
1979and assigned sequentially.
1980When a shell function is invoked,
1981any arguments passed to it are temporarily reassigned to the
1982positional parameters;
1983when the function completes,
1984the values are restored.
1985Positional parameters 10 and above should be enclosed in {}.
1986Positional parameters can be reassigned using the
1987.Ic set
1988command.
1989.It @
1990All positional parameters.
1991Within double quotes,
1992each parameter is output as a separate field.
1993The resulting list completely matches what was passed to the shell.
1994So "1 2" "3" is output as two parameters, "1 2" and "3".
1995.It *
1996All positional parameters.
1997Within double quotes,
1998all parameters are output as one field,
1999separated by the first character of
2000.Ev IFS
2001(by default a space).
2002The resulting list of words is amalgamated,
2003losing the sense of how they were passed to the shell.
2004So "1 2" "3" is output as one parameter, "1 2 3".
2005.It #
2006The number of positional parameters.
2007.It ?
2008The exit status of the most recent command.
2009.It -
2010The current shell options.
2011.It $
2012The process ID of the current shell.
2013Subshells have the same PID as the current shell.
2014.It !
2015The process ID of the most recent background command.
2016.El
2017.Sh ENVIRONMENT
2018The following environment variables affect the execution of
2019.Nm :
2020.Bl -tag -width "POSIXLY_CORRECT"
2021.It Ev CDPATH
2022Colon separated list of directories used by the
2023.Ic cd
2024command.
2025If unset or empty,
2026the current working directory is used.
2027.It Ev ENV
2028Pathname to a file containing commands to be executed
2029when an interactive shell is started.
2030.It Ev FCEDIT
2031Editor for the
2032.Ic fc
2033builtin.
2034The default is
2035.Xr ed 1 .
2036.It Ev HISTFILE
2037Pathname to a file to be used to record command history.
2038The default is to not write command history to a file.
2039.It Ev HISTSIZE
2040The maximum number of commands stored in history.
2041The default is 500.
2042.It Ev HOME
2043Pathname to a user's home directory.
2044.It Ev IFS
2045A list of characters to be used for field splitting.
2046.It Ev LINENO
2047The current line number in a script or function,
2048starting at 1.
2049This variable should not be set by users.
2050.It Ev MAIL
2051Pathname to a user's mailbox file.
2052If set,
2053.Nm
2054reports the arrival of new mail
2055(ascertained by checking a file's modification time)
2056every
2057.Ev MAILCHECK
2058seconds.
2059.Ev MAIL
2060is overridden by
2061.Ev MAILPATH .
2062.It Ev MAILCHECK
2063How often,
2064in seconds,
2065to check for new mail in either
2066.Ev MAIL
2067or
2068.Ev MAILPATH .
2069The default is 600 (10 minutes).
2070If set to 0,
2071check before issuing each prompt.
2072.It Ev MAILPATH
2073Pathname to a colon separated list of mailboxes.
2074If set,
2075.Nm
2076reports the arrival of new mail
2077(ascertained by checking a file's modification time)
2078every
2079.Ev MAILCHECK
2080seconds.
2081The default notification message
2082.Pq Qq you have mail in $_
2083can be changed per mailbox by appending
2084.No % Ns Ar message
2085to a pathname.
2086.Ev MAILPATH
2087overrides
2088.Ev MAIL .
2089.It Ev OLDPWD
2090Pathname to the previous working directory.
2091.It Ev OPTARG
2092An option argument for the
2093.Ic getopts
2094command.
2095.It Ev OPTIND
2096An index to the next option for the
2097.Ic getopts
2098command.
2099.It Ev PATH
2100Pathname to a colon separated list of directories
2101used to search for the location of executable files.
2102A pathname of
2103.Sq .\&
2104represents the current working directory.
2105The default value of
2106.Ev PATH
2107on
2108.Ox
2109is:
2110.Bd -literal -offset 2n
2111/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin
2112.Ed
2113.It Ev POSIXLY_CORRECT
2114Enable POSIX mode
2115(see
2116.Sx STANDARDS ) .
2117.It Ev PPID
2118The shell's parent process ID.
2119Subshells have the same
2120.Ev PPID
2121as the current shell.
2122.It Ev PS1
2123User prompt displayed every time an interactive shell
2124is ready to read a command.
2125A
2126.Sq !\&
2127in the prompt is expanded to the number of the next command in history
2128to be typed.
2129.It Ev PS2
2130Newline prompt displayed in an interactive shell
2131when a newline has been entered
2132before the command line completes.
2133The default value is
2134.Sq >\ \& .
2135.It Ev PS4
2136Trace prompt displayed in an interactive shell
2137before each command is traced
2138(see the
2139.Fl x
2140option).
2141The default is
2142.Sq +\ \& .
2143.It PWD
2144The absolute pathname to the current working directory.
2145.El
2146.Sh ASYNCHRONOUS EVENTS
2147The following signals affect the execution of
2148.Nm :
2149.Bl -tag -width "SIGQUITXXX"
2150.It Dv SIGINT
2151If a shell is interactive
2152and in command line editing mode,
2153editing is terminated on the current line
2154and the command being edited is not entered into command history.
2155Otherwise the signal is caught
2156but no action is taken.
2157.It Dv SIGQUIT
2158Ignored if a shell is interactive.
2159.It Dv SIGTERM
2160Ignored if a shell is interactive.
2161.It Dv SIGTSTP
2162Ignored if a shell is interactive
2163and the
2164.Ic monitor
2165option
2166.Pq Fl m
2167is set.
2168.It Dv SIGTTIN
2169Ignored if a shell is interactive
2170and the
2171.Ic monitor
2172option
2173.Pq Fl m
2174is set.
2175.It Dv SIGTTOU
2176Ignored if a shell is interactive
2177and the
2178.Ic monitor
2179option
2180.Pq Fl m
2181is set.
2182.El
2183.Sh EXIT STATUS
2184The
2185.Nm
2186utility exits with one of:
2187.Bl -tag -width "1-125"
2188.It 0
2189The script being executed contained only blank lines or comments.
2190.It 1\(en125
2191A non-interactive shell detected an error other than
2192.Ar file
2193not found.
2194.It 126
2195A command was found but was not executable.
2196.It 127
2197A non-interactive shell returned
2198.Ar file
2199not found.
2200.El
2201.Pp
2202Otherwise
2203.Nm
2204returns the exit status of the last command it invoked.
2205.Sh SEE ALSO
2206.Xr csh 1 ,
2207.Xr ed 1 ,
2208.Xr ksh 1 ,
2209.Xr vi 1 ,
2210.Xr script 7
2211.Sh STANDARDS
2212The
2213.Nm
2214utility is compliant with the
2215.St -p1003.1-2008
2216specification,
2217except where noted below:
2218.Bl -dash
2219.It
2220The flag
2221.Op Fl h
2222is documented by POSIX as hashing
2223.Qq utilities invoked by functions as those functions are defined ;
2224this implementation hashes utilities after first invocation
2225(and functions be damned).
2226.It
2227POSIX says mail notifications via
2228.Ev MAIL
2229and
2230.Ev MAILPATH
2231should happen if a file is created,
2232as well as if its modification time changes.
2233This implementation of
2234.Nm
2235does not provide notification when these files are created.
2236.It
2237The built-in
2238.Ic newgrp
2239is unsupported.
2240.It
2241The
2242.Ic break
2243and
2244.Ic continue
2245built-ins should exit/return from the outermost loop if the argument
2246.Ar n
2247is greater than the level of loops.
2248.It
2249The default value for the
2250.Ev PS1
2251user prompt contains the machine's hostname,
2252followed by
2253.Sq $\ \&
2254for normal users and
2255.Sq #\ \&
2256for root;
2257POSIX does not include the hostname.
2258.El
2259.Pp
2260Enabling POSIX mode changes some behaviour to make
2261.Nm
2262adhere more strictly to the
2263.St -p1003.1-2008
2264specification.