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