Skip to content
xcu_chap02.html 127 KiB
Newer Older
Seblu's avatar
Seblu committed
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
echo ${x##*/}
</tt><b>three</b>
</pre>
</dd>
</dl>

<p>The double-quoting of patterns is different depending on where the double-quotes are placed:</p>

<dl compact="compact">
<dt><tt>"${x#*}"</tt></dt>

<dd>The asterisk is a pattern character.</dd>

<dt><tt>${x#"*"}</tt></dt>

<dd>The literal asterisk is quoted and not special.</dd>
</dl>

<div class="box"><em>End of informative text.</em></div>

<hr>
<h4><a name="tag_02_06_03">  2.6.3 </a>Command Substitution</h4>

<p>Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution
shall occur when the command is enclosed as follows:</p>

<blockquote>
<pre><tt>$(</tt><i>command</i><tt>)
</tt>
</pre>
</blockquote>

<p>or (backquoted version):</p>

<blockquote>
<pre><tt>`</tt><i>command</i><tt>`
</tt>
</pre>
</blockquote>

<p>The shell shall expand the command substitution by executing <i>command</i> in a subshell environment (see <a href="#tag_02_12">Shell Execution Environment</a>) and replacing the command substitution (the text of <i>command</i> plus the
enclosing <tt>"$()"</tt>
or backquotes) with the standard output of the command, removing
sequences of one or more &lt;newline&gt;s
at the end of the substitution. Embedded &lt;newline&gt;s before the
end of the output shall not be removed; however, they may be
treated as field delimiters and eliminated during field splitting,
depending on the value of <i>IFS</i> and quoting that is in
effect.</p>

<p>Within the backquoted style of command substitution, backslash shall retain its literal meaning, except when followed by:
<tt>'$'</tt>, <tt>'`'</tt>, or <tt>'\'</tt> (dollar sign, backquote,
backslash). The search for the matching backquote shall be
satisfied by the first backquote found without a preceding backslash;
during this search, if a non-escaped backquote is encountered
within a shell comment, a here-document, an embedded command
substitution of the $( <i>command</i>) form, or a quoted string,
undefined results occur. A single-quoted or double-quoted string that begins, but does not end, within the <tt>"`...`"</tt>
sequence produces undefined results.</p>

<p>With the $( <i>command</i>) form, all characters following the open parenthesis to the matching closing parenthesis constitute
the <i>command</i>. Any valid shell script can be used for <i>command</i>, except a script consisting solely of redirections which
produces unspecified results.</p>

<p>The results of command substitution shall not be processed for further tilde expansion, parameter expansion, command
substitution, or arithmetic expansion. If a command substitution occurs inside double-quotes, field splitting and pathname
expansion shall not be performed on the results of the substitution.</p>

<p>Command substitution can be nested. To specify nesting within the backquoted version, the application shall precede the inner
backquotes with backslashes, for example:</p>

<blockquote>
<pre><tt>\`</tt><i>command</i><tt>\`
</tt>
</pre>
</blockquote>

<p>If the command substitution consists of a single subshell, such as:</p>

<blockquote>
<pre><tt>$( (</tt><i>command</i><tt>) )
</tt>
</pre>
</blockquote>

<p>a conforming application shall separate the <tt>"$("</tt> and <tt>'('</tt> into two tokens (that is, separate them with white
space). This is required to avoid any ambiguities with arithmetic expansion.</p>

<h4><a name="tag_02_06_04">  2.6.4 </a>Arithmetic Expansion</h4>

<p>Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for
arithmetic expansion shall be as follows:</p>

<blockquote>
<pre><tt>$((</tt><i>expression</i><tt>))
</tt>
</pre>
</blockquote>

<p>The expression shall be treated as if it were in double-quotes, except that a double-quote inside the expression is not treated
specially. The shell shall expand all tokens in the expression for parameter expansion, command substitution, and quote
removal.</p>

<p>Next, the shell shall treat this as an arithmetic expression and substitute the value of the expression. The arithmetic
expression shall be processed according to the rules given in <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap01.html#tag_01_07_02_01"><i>Arithmetic Precision and
Operations</i></a> , with the following exceptions:</p>

<ul>
<li>
<p>Only signed long integer arithmetic is required.</p>
</li>

<li>
<p>Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO&nbsp;C standard, Section
6.4.4.1 are required to be recognized as constants.</p>
</li>

<li>
<p>The <i>sizeof</i>() operator and the prefix and postfix <tt>"++"</tt> and <tt>"--"</tt> operators are not required.</p>
</li>

<li>
<p>Selection, iteration, and jump statements are not supported.</p>
</li>
</ul>

<p>All changes to variables in an arithmetic expression shall be in effect after the arithmetic expansion, as in the parameter
expansion <tt>"${x=value}"</tt>.</p>

<p>If the shell variable <i>x</i> contains a value that forms a valid integer constant, then the arithmetic expansions
<tt>"$((x))"</tt> and <tt>"$(($x))"</tt> shall return the same value.</p>

<p>As an extension, the shell may recognize arithmetic expressions beyond those listed. The shell may use a signed integer type
with a rank larger than the rank of <b>signed long</b>. The shell may use a real-floating type instead of <b>signed long</b>
as
long as it does not affect the results in cases where there is no
overflow. If the expression is invalid, the expansion fails and
the shell shall write a message to standard error indicating the
failure.</p>


<hr>
<div class="box"><tt><em>The following sections are informative.</em></tt></div>

<h5><tt><a name="tag_02_06_04_01"></a>Examples</tt></h5>

<p><tt>A simple example using arithmetic expansion:</tt></p>

<blockquote>
<pre><tt># repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
   </tt> <i>command</i> <tt>   x=$(($x-1))
done
</tt>
</pre>
</blockquote>

<div class="box"><em>End of informative text.</em></div>

<hr>
<h4><a name="tag_02_06_05">  2.6.5 </a>Field Splitting</h4>

<p>After parameter expansion ( <a href="#tag_02_06_02">Parameter Expansion</a>), command substitution ( <a href="#tag_02_06_03">Command Substitution</a>), and arithmetic expansion ( <a href="#tag_02_06_04">Arithmetic Expansion</a>),
the
shell shall scan the results of expansions and substitutions that did
not occur in double-quotes for field splitting and multiple
fields can result.</p>

<p>The shell shall treat each character of the <i>IFS</i> as a delimiter and use the delimiters to split the results of parameter
expansion and command substitution into fields.</p>

<ol>
<li>
<p>If the value of <i>IFS</i> is a &lt;space&gt;, &lt;tab&gt;, and
&lt;newline&gt;, or if it is unset, any sequence of
&lt;space&gt;s, &lt;tab&gt;s, or &lt;newline&gt;s at the beginning or
end of the input shall be ignored and any sequence of those
characters within the input shall delimit a field. For example, the
input:</p>

<blockquote>
<pre><tt>&lt;newline&gt;&lt;space&gt;&lt;tab&gt;foo&lt;tab&gt;&lt;tab&gt;bar&lt;space&gt;
</tt>
</pre>
</blockquote>

<p>yields two fields, <b>foo</b> and <b>bar</b>.</p>
</li>

<li>
<p>If the value of <i>IFS</i> is null, no field splitting shall be performed.</p>
</li>

<li>
<p>Otherwise, the following rules shall be applied in sequence. The term " <i>IFS</i> white space" is used to mean any sequence
(zero or more instances) of white space characters that are in the <i>IFS</i> value (for example, if <i>IFS</i> contains
&lt;space&gt;/ &lt;comma&gt;/ &lt;tab&gt;, any sequence of &lt;space&gt;s and &lt;tab&gt;s is considered <i>IFS</i> white
space).</p>

<ol type="a">
<li>
<p><i>IFS</i> white space shall be ignored at the beginning and end of the input.</p>
</li>

<li>
<p>Each occurrence in the input of an <i>IFS</i> character that is not <i>IFS</i> white space, along with any adjacent <i>IFS</i>
white space, shall delimit a field, as described previously.</p>
</li>

<li>
<p>Non-zero-length <i>IFS</i> white space shall delimit a field.</p>
</li>
</ol>
</li>
</ol>

<h4><a name="tag_02_06_06">  2.6.6 </a>Pathname Expansion</h4>

<p>After field splitting, if <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/set.html"><i>set</i></a> <b>-f</b> is not in effect, each field in the resulting command line
shall be expanded using the algorithm described in <a href="#tag_02_13">Pattern Matching Notation</a> , qualified by the rules in
<a href="#tag_02_13_03">Patterns Used for Filename Expansion</a>.</p>

<h4><a name="tag_02_06_07">  2.6.7 </a>Quote Removal</h4>

<p>The quote characters: <tt>'\'</tt>, <tt>'"</tt>, and <tt>''</tt> (backslash, single-quote, double-quote) that were present in
the original word shall be removed unless they have themselves been quoted.</p>

<h3><a name="tag_02_07">   2.7 </a>Redirection</h3>

<p>Redirection is used to open and close files for the current shell execution environment (see <a href="#tag_02_12">Shell
Execution Environment</a>) or for any command. Redirection operators can be used with numbers representing file descriptors (see
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_165">Section 3.165, File
Descriptor</a>) as described below.</p>

<p>The overall format used for redirection is:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><i>redir-op word</i>
</pre>
</blockquote>

<p>The number <i>n</i> is an optional decimal number designating the file descriptor number; the application shall ensure it is
delimited from any preceding text and immediately precede the redirection operator <i>redir-op</i>. If <i>n</i> is quoted, the
number shall not be recognized as part of the redirection expression. For example:</p>

<blockquote>
<pre><tt>echo \2&gt;a
</tt>
</pre>
</blockquote>

<p>writes the character 2 into file <b>a</b>. If any part of <i>redir-op</i> is quoted, no redirection expression is recognized.
For example:</p>

<blockquote>
<pre><tt>echo 2\&gt;a
</tt>
</pre>
</blockquote>

<p>writes the characters 2&gt;<i>a</i> to standard output. The optional number, redirection operator, and <i>word</i> shall not
appear in the arguments provided to the command to be executed (if any).</p>

<p>Open files are represented by decimal numbers starting with zero.
The largest possible value is implementation-defined; however,
all implementations shall support at least 0 to 9, inclusive, for use
by the application. These numbers are called "file
descriptors". The values 0, 1, and 2 have special meaning and
conventional uses and are implied by certain redirection operations;
they are referred to as <i>standard input</i>, <i>standard output</i>, and <i>standard error</i>,
respectively. Programs usually
take their input from standard input, and write output on standard
output. Error messages are usually written on standard error.
The redirection operators can be preceded by one or more digits (with
no intervening &lt;blank&gt;s allowed) to designate the file
descriptor number.</p>

<p>If the redirection operator is <tt>"&lt;&lt;"</tt> or <tt>"&lt;&lt;-"</tt>,
the word that follows the redirection operator
shall be subjected to quote removal; it is unspecified whether any of
the other expansions occur. For the other redirection
operators, the word that follows the redirection operator shall be
subjected to tilde expansion, parameter expansion, command
substitution, arithmetic expansion, and quote removal. Pathname
expansion shall not be performed on the word by a non-interactive
shell; an interactive shell may perform it, but shall do so only when
the expansion would result in one word.</p>

<p>If more than one redirection operator is specified with a command, the order of evaluation is from beginning to end.</p>

<p>A failure to open or create a file shall cause a redirection to fail.</p>

<h4><a name="tag_02_07_01">  2.7.1 </a>Redirecting Input</h4>

<p>Input redirection shall cause the file whose name results from the expansion of <i>word</i> to be opened for reading on the
designated file descriptor, or standard input if the file descriptor is not specified.</p>

<p>The general format for redirecting input is:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&lt;</tt><i>word</i>
</pre>
</blockquote>

<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection shall refer to
standard input (file descriptor 0).</p>

<h4><a name="tag_02_07_02">  2.7.2 </a>Redirecting Output</h4>

<p>The two general formats for redirecting output are:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&gt;</tt><i>word</i>
<b>[</b><i>n</i><b>]</b><tt>&gt;|</tt><i>word</i>
</pre>
</blockquote>

<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection shall refer to
standard output (file descriptor 1).</p>

<p>Output redirection using the <tt>'&gt;'</tt> format shall fail if the <i>noclobber</i> option is set (see the description of <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/set.html"><i>set</i></a> <b>-C</b>) and the file named by the expansion of <i>word</i> exists and is a regular file. Otherwise,
redirection using the <tt>'&gt;'</tt> or <tt>"&gt;|"</tt> formats shall cause the file whose name results from the expansion of
<i>word</i> to be created and opened for output on the designated file descriptor, or standard output if none is specified. If the
file does not exist, it shall be created; otherwise, it shall be truncated to be an empty file after being opened.</p>

<h4><a name="tag_02_07_03">  2.7.3 </a>Appending Redirected Output</h4>

<p>Appended output redirection shall cause the file whose name results from the expansion of word to be opened for output on the
designated file descriptor. The file is opened as if the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/open.html"><i>open</i>()</a> function as defined in
the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 was called with the O_APPEND flag. If the file does not exist, it
shall be created.</p>

<p>The general format for appending redirected output is as follows:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&gt;&gt;</tt><i>word</i>
</pre>
</blockquote>

<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection refers to standard
output (file descriptor 1).</p>

<h4><a name="tag_02_07_04">  2.7.4 </a>Here-Document</h4>

<p>The redirection operators <tt>"&lt;&lt;"</tt> and <tt>"&lt;&lt;-"</tt> both allow redirection of lines contained in a shell
input file, known as a "here-document", to the input of a command.</p>

<p>The here-document shall be treated as a single word that begins
after the next &lt;newline&gt; and continues until there is a
line containing only the delimiter and a &lt;newline&gt;, with no
&lt;blank&gt;s in between. Then the next here-document starts, if
there is one. The format is as follows:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&lt;&lt;</tt><i>word
    here-document
delimiter</i>
</pre>
</blockquote>

<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the here-document refers to
standard input (file descriptor 0).</p>

<p>If any character in <i>word</i> is quoted, the delimiter shall be formed by performing quote removal on <i>word</i>, and the
here-document lines shall not be expanded. Otherwise, the delimiter shall be the <i>word</i> itself.</p>

<p>If no characters in <i>word</i> are quoted, all lines of the
here-document shall be expanded for parameter expansion, command
substitution, and arithmetic expansion. In this case, the backslash in
the input behaves as the backslash inside double-quotes (see
<a href="#tag_02_02_03">Double-Quotes</a>). However, the double-quote character ( <tt>' )'</tt> shall not be treated specially
within a here-document, except when the double-quote appears within <tt>"$()"</tt>, <tt>"``"</tt>, or <tt>"${}"</tt>.</p>

<p>If the redirection symbol is <tt>"&lt;&lt;-"</tt>, all leading &lt;tab&gt;s shall be stripped from input lines and the line
containing the trailing delimiter. If more than one <tt>"&lt;&lt;"</tt> or <tt>"&lt;&lt;-"</tt> operator is specified on a line,
the here-document associated with the first operator shall be supplied first by the application and shall be read first by the
shell.</p>

<hr>
<div class="box"><em>The following sections are informative.</em></div>

<h5><a name="tag_02_07_04_01"></a>Examples</h5>

<p>An example of a here-document follows:</p>

<blockquote>
<pre><tt>cat &lt;&lt;eof1; cat &lt;&lt;eof2
Hi,
eof1
Helene.
eof2
</tt>
</pre>
</blockquote>

<div class="box"><em>End of informative text.</em></div>

<hr>
<h4><a name="tag_02_07_05">  2.7.5 </a>Duplicating an Input File Descriptor</h4>

<p>The redirection operator:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&lt;&amp;</tt><i>word</i>
</pre>
</blockquote>

<p>shall duplicate one input file descriptor from another, or shall close one. If <i>word</i> evaluates to one or more digits, the
file descriptor denoted by <i>n</i>, or standard input if <i>n</i> is not specified, shall be made to be a copy of the file
descriptor denoted by <i>word</i>; if the digits in <i>word</i> do not represent a file descriptor already open for input, a
redirection error shall result; see <a href="#tag_02_08_01">Consequences of Shell Errors</a>. If <i>word</i> evaluates to
<tt>'-'</tt>, file descriptor <i>n</i>, or standard input if <i>n</i> is not specified, shall be closed. Attempts to close a file
descriptor that is not open shall not constitute an error. If <i>word</i> evaluates to something else, the behavior is
unspecified.</p>

<h4><a name="tag_02_07_06">  2.7.6 </a>Duplicating an Output File Descriptor</h4>

<p>The redirection operator:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&gt;&amp;</tt><i>word</i>
</pre>
</blockquote>

<p>shall duplicate one output file descriptor from another, or shall close one. If <i>word</i> evaluates to one or more digits, the
file descriptor denoted by <i>n</i>, or standard output if <i>n</i> is not specified, shall be made to be a copy of the file
descriptor denoted by <i>word</i>; if the digits in <i>word</i> do not represent a file descriptor already open for output, a
redirection error shall result; see <a href="#tag_02_08_01">Consequences of Shell Errors</a>. If <i>word</i> evaluates to
<tt>'-'</tt>, file descriptor <i>n</i>, or standard output if <i>n</i> is not specified, is closed. Attempts to close a file
descriptor that is not open shall not constitute an error. If <i>word</i> evaluates to something else, the behavior is
unspecified.</p>

<h4><a name="tag_02_07_07">  2.7.7 </a>Open File Descriptors for Reading and Writing</h4>

<p>The redirection operator:</p>

<blockquote>
<pre><b>[</b><i>n</i><b>]</b><tt>&lt;&gt;</tt><i>word</i>
</pre>
</blockquote>

<p>shall cause the file whose name is the expansion of <i>word</i> to be opened for both reading and writing on the file descriptor
denoted by <i>n</i>, or standard input if <i>n</i> is not specified. If the file does not exist, it shall be created.</p>

<h3><a name="tag_02_08">   2.8 </a>Exit Status and Errors</h3>

<h4><a name="tag_02_08_01">  2.8.1 </a>Consequences of Shell Errors</h4>

<p>For a non-interactive shell, an error condition encountered by a special built-in (see <a href="#tag_02_14">Special Built-In
Utilities</a>) or other type of utility shall cause the shell to write a diagnostic message to standard error and exit as shown in
the following table:</p>

<center>
<table align="center" border="1" cellpadding="3">
<tbody><tr valign="top">
<th align="center">
<p class="tent"><b>Error</b></p>
</th>
<th align="center">
<p class="tent"><b>Special Built-In</b></p>
</th>
<th align="center">
<p class="tent"><b>Other Utilities</b></p>
</th>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Shell language syntax error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Utility syntax error (option or operand error)</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Redirection error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Variable assignment error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Expansion error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Command not found</p>
</td>
<td align="left">
<p class="tent">N/A</p>
</td>
<td align="left">
<p class="tent">May exit</p>
</td>
</tr>

<tr valign="top">
<td align="left">
<p class="tent">Dot script not found</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">N/A</p>
</td>
</tr>
</tbody></table>
</center>

<p>An expansion error is one that occurs when the shell expansions defined in <a href="#tag_02_06">Word Expansions</a> are carried
out (for example, <tt>"${x!y}"</tt>, because <tt>'!'</tt> is not a valid operator); an implementation may treat these as syntax
errors if it is able to detect them during tokenization, rather than during expansion.</p>

<p>If any of the errors shown as "shall exit" or "(may) exit" occur in a subshell, the subshell shall (respectively may) exit
with a non-zero status, but the script containing the subshell shall not exit because of the error.</p>

<p>In all of the cases shown in the table, an interactive shell shall write a diagnostic message to standard error without
exiting.</p>

<h4><a name="tag_02_08_02">  2.8.2 </a>Exit Status for Commands</h4>

<p>Each command has an exit status that can influence the behavior of other shell commands. The exit status of commands that are
not utilities is documented in this section. The exit status of the standard utilities is documented in their respective
sections.</p>

<p>If a command is not found, the exit status shall be 127. If the
command name is found, but it is not an executable utility, the
exit status shall be 126. Applications that invoke utilities without
using the shell should use these exit status values to report
similar errors.</p>

<p>If a command fails during word expansion or redirection, its exit status shall be greater than zero.</p>

<p>Internally, for purposes of deciding whether a command exits with a non-zero exit status, the shell shall recognize the entire
status value retrieved for the command by the equivalent of the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/wait.html"><i>wait</i>()</a> function
WEXITSTATUS macro (as defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001). When reporting the exit status
with the special parameter <tt>'?'</tt>, the shell shall report the full eight bits of exit status available. The exit status of a
command that terminated because it received a signal shall be reported as greater than 128.</p>

<h3><a name="tag_02_09">   2.9 </a>Shell Commands</h3>

<p>This section describes the basic structure of shell commands. The following command descriptions each describe a format of the
command that is only used to aid the reader in recognizing the command type, and does not formally represent the syntax. Each
description discusses the semantics of the command; for a formal definition of the command language, consult <a href="#tag_02_10">Shell Grammar</a>.</p>

<p>A <i>command</i> is one of the following:</p>

<ul>
<li>
<p>Simple command (see <a href="#tag_02_09_01">Simple Commands</a>)</p>
</li>

<li>
<p>Pipeline (see <a href="#tag_02_09_02">Pipelines</a>)</p>
</li>

<li>
<p>List compound-list (see <a href="#tag_02_09_03">Lists</a>)</p>
</li>

<li>
<p>Compound command (see <a href="#tag_02_09_04">Compound Commands</a>)</p>
</li>

<li>
<p>Function definition (see <a href="#tag_02_09_05">Function Definition Command</a>)</p>
</li>
</ul>

<p>Unless otherwise stated, the exit status of a command shall be that
of the last simple command executed by the command. There
shall be no limit on the size of any shell command other than that
imposed by the underlying system (memory constraints, {ARG_MAX},
and so on).</p>

<h4><a name="tag_02_09_01">  2.9.1 </a>Simple Commands</h4>

<p>A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by
words and redirections, terminated by a control operator.</p>

<p>When a given simple command is required to be executed (that is, when any conditional construct such as an AND-OR list or a
<b>case</b> statement has not bypassed the simple command), the following expansions, assignments, and redirections shall all be
performed from the beginning of the command text to the end:</p>

<ol>
<li>
<p>The words that are recognized as variable assignments or redirections according to <a href="#tag_02_10_02">Shell Grammar
Rules</a> are saved for processing in steps 3 and 4.</p>
</li>

<li>
<p>The words that are not variable assignments or redirections shall be expanded. If any fields remain following their expansion,
the first field shall be considered the command name and remaining fields are the arguments for the command.</p>
</li>

<li>
<p>Redirections shall be performed as described in <a href="#tag_02_07">Redirection</a>.</p>
</li>

<li>
<p>Each variable assignment shall be expanded for tilde expansion, parameter expansion, command substitution, arithmetic expansion,
and quote removal prior to assigning the value.</p>
</li>
</ol>

<p>In the preceding list, the order of steps 3 and 4 may be reversed for the processing of special built-in utilities; see <a href="#tag_02_14">Special Built-In Utilities</a>.</p>

<p>If no command name results, variable assignments shall affect the
current execution environment. Otherwise, the variable
assignments shall be exported for the execution environment of the
command and shall not affect the current execution environment
(except for special built-ins). If any of the variable assignments
attempt to assign a value to a read-only variable, a variable
assignment error shall occur. See <a href="#tag_02_08_01">Consequences of Shell Errors</a> for the consequences of these
errors.</p>

<p>If there is no command name, any redirections shall be performed in
a subshell environment; it is unspecified whether this
subshell environment is the same one as that used for a command
substitution within the command. (To affect the current execution
environment, see the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/exec.html#tag_04_46"><i>exec</i>()</a>
special built-in.) If any of the redirections performed in the
current shell execution environment fail, the command shall immediately
fail with an exit status greater than zero, and the shell
shall write an error message indicating the failure. See <a href="#tag_02_08_01">Consequences of Shell Errors</a> for the
consequences of these failures on interactive and non-interactive shells.</p>

<p>If there is a command name, execution shall continue as described in <a href="#tag_02_09_01_01">Command Search and Execution</a>
. If there is no command name, but the command contained a command
substitution, the command shall complete with the exit status of
the last command substitution performed. Otherwise, the command shall
complete with a zero exit status.</p>

<h5><a name="tag_02_09_01_01"></a>Command Search and Execution</h5>

<p>If a simple command results in a command name and an optional list of arguments, the following actions shall be performed:</p>

<ol>
<li>
<p>If the command name does not contain any slashes, the first successful step in the following sequence shall occur:</p>

<ol type="a">
<li>
<p>If the command name matches the name of a special built-in utility, that special built-in utility shall be invoked.</p>
</li>

<li>
<p>If the command name matches the name of a function known to this shell, the function shall be invoked as described in <a href="#tag_02_09_05">Function Definition Command</a>. If the implementation has provided a standard utility in the form of a function,
it shall not be recognized at this point. It shall be invoked in conjunction with the path search in step 1d.</p>
</li>

<li>
<p>If the command name matches the name of a utility listed in the following table, that utility shall be invoked.</p>

<center>
<table align="center" cellpadding="3">
<tbody><tr valign="top">
<td align="left">
<p class="tent"><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/alias.html"><i>alias</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/bg.html"><i>bg</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/cd.html"><i>cd</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/command.html"><i>command</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/false.html"><i>false</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/fc.html"><i>fc</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/fg.html"><i>fg</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/getopts.html"><i>getopts</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/jobs.html"><i>jobs</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html"><i>kill</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/newgrp.html"><i>newgrp</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/pwd.html"><i>pwd</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/read.html"><i>read</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/true.html"><i>true</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/umask.html"><i>umask</i></a><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/unalias.html"><i>unalias</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="http://www.opengroup.org/onlinepubs/009695399/utilities/wait.html"><i>wait</i></a><br>
&nbsp;</p>
</td>
</tr>
</tbody></table>
</center>
</li>

<li>
<p>Otherwise, the command shall be searched for using the <i>PATH</i> environment variable as described in the Base Definitions
volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>:</p>

<ol type="i">
<li>
<p>If the search is successful:</p>

<ol type="a">
<li>
<p>If the system has implemented the utility as a regular built-in or as a shell function, it shall be invoked at this point in the
path search.</p>
</li>

<li>
<p>Otherwise, the shell executes the utility in a separate utility environment (see <a href="#tag_02_12">Shell Execution
Environment</a>) with actions equivalent to calling the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/execve.html"><i>execve</i>()</a> function as defined
in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 with the <i>path</i> argument set to the pathname resulting from
the search, <i>arg</i>0 set to the command name, and the remaining arguments set to the operands, if any.</p>

<p>If the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/execve.html"><i>execve</i>()</a>
function fails due to an error equivalent to the [ENOEXEC] error
defined in the System Interfaces volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, the shell shall execute a command
equivalent to having a
shell invoked with the pathname resulting from the search as its first
operand, with any remaining arguments passed to the new
shell, except that the value of <tt>"$0"</tt> in the new shell may be
set to the command name. If the executable file is not a text
file, the shell may bypass this command execution. In this case, it
shall write an error message, and shall return an exit status
of 126.</p>
</li>
</ol>

<p>Once a utility has been searched for and found (either as a result of this specific search or as part of an unspecified shell
start-up activity), an implementation may remember its location and need not search for the utility again unless the <i>PATH</i>
variable has been the subject of an assignment. If the remembered location fails for a subsequent invocation, the shell shall
repeat the search to find the new location for the utility, if any.</p>
</li>

<li>
<p>If the search is unsuccessful, the command shall fail with an exit status of 127 and the shell shall write an error message.</p>
</li>
</ol>
</li>
</ol>
</li>

<li>
<p>If the command name contains at least one slash, the shell shall execute the utility in a separate utility environment with
actions equivalent to calling the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/execve.html"><i>execve</i>()</a> function defined in the System Interfaces
volume of IEEE&nbsp;Std&nbsp;1003.1-2001 with the <i>path</i> and <i>arg</i>0 arguments set to the command name, and the remaining
arguments set to the operands, if any.</p>

<p>If the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/execve.html"><i>execve</i>()</a> function fails due to an error equivalent to the [ENOEXEC] error,
the shell shall execute a command equivalent to having a shell invoked with the command name as its first operand, with any
remaining arguments passed to the new shell. If the executable file is not a text file, the shell may bypass this command
execution. In this case, it shall write an error message and shall return an exit status of 126.</p>
</li>
</ol>

<h4><a name="tag_02_09_02">  2.9.2 </a>Pipelines</h4>

<p>A <i>pipeline</i> is a sequence of one or more commands separated by the control operator <tt>'|'</tt>. The standard output of
all but the last command shall be connected to the standard input of the next command.</p>

<p>The format for a pipeline is:</p>

<blockquote>
<pre><b>[</b><tt>!</tt><b>]</b> <i>command1</i> <b>[</b> <tt>|</tt> <i>command2</i> <tt>...</tt><b>]</b>
</pre>
</blockquote>

<p>The standard output of <i>command1</i> shall be connected to the standard input of <i>command2</i>. The standard input, standard
output, or both of a command shall be considered to be assigned by the pipeline before any redirection specified by redirection
operators that are part of the command (see <a href="#tag_02_07">Redirection</a>).</p>

<p>If the pipeline is not in the background (see <a href="#tag_02_09_03_02">Asynchronous Lists</a>), the shell shall wait for the
last command specified in the pipeline to complete, and may also wait for all commands to complete.</p>

<h5><a name="tag_02_09_02_01"></a>Exit Status</h5>

<p>If the reserved word <b>!</b> does not precede the pipeline, the
exit status shall be the exit status of the last command
specified in the pipeline. Otherwise, the exit status shall be the
logical NOT of the exit status of the last command. That is, if
the last command returns zero, the exit status shall be 1; if the last
command returns greater than zero, the exit status shall be
zero.</p>

<h4><a name="tag_02_09_03">  2.9.3 </a>Lists</h4>

<p>An <i>AND-OR list</i> is a sequence of one or more pipelines separated by the operators <tt>"&amp;&amp;"</tt> and <tt>"||"</tt>
.</p>

<p>A <i>list</i> is a sequence of one or more AND-OR lists separated by the operators <tt>';'</tt> and <tt>'&amp;'</tt> and
optionally terminated by <tt>';'</tt>, <tt>'&amp;'</tt>, or &lt;newline&gt;.</p>

<p>The operators <tt>"&amp;&amp;"</tt> and <tt>"||"</tt> shall have equal precedence and shall be evaluated with left
associativity. For example, both of the following commands write solely <b>bar</b> to standard output:</p>

<blockquote>
<pre><tt>false &amp;&amp; echo foo || echo bar
true || echo foo &amp;&amp; echo bar
</tt>
</pre>
</blockquote>

<p>A <tt>';'</tt> or &lt;newline&gt; terminator shall cause the preceding AND-OR list to be executed sequentially; an
<tt>'&amp;'</tt> shall cause asynchronous execution of the preceding AND-OR list.</p>

<p>The term "compound-list" is derived from the grammar in <a href="#tag_02_10">Shell Grammar</a>; it is equivalent to a
sequence of <i>lists</i>, separated by &lt;newline&gt;s, that can be preceded or followed by an arbitrary number of
&lt;newline&gt;s.</p>

<hr>
<div class="box"><em>The following sections are informative.</em></div>

<h5><a name="tag_02_09_03_01"></a>Examples</h5>

<p>The following is an example that illustrates &lt;newline&gt;s in compound-lists:</p>

<blockquote>
<pre><tt>while
    # a couple of &lt;newline&gt;s
<br>
    # a list
    date &amp;&amp; who || ls; cat file
    # a couple of &lt;newline&gt;s
<br>
    # another list
    wc file &gt; output &amp; true
<br>
do
    # 2 lists
    ls
    cat file
done
</tt>
</pre>
</blockquote>

<div class="box"><em>End of informative text.</em></div>

<hr>
<h5><a name="tag_02_09_03_02"></a>Asynchronous Lists</h5>

<p>If a command is terminated by the control operator ampersand ( <tt>'&amp;'</tt> ), the shell shall execute the command
asynchronously in a subshell. This means that the shell shall not wait for the command to finish before executing the next
command.</p>

<p>The format for running a command in the background is:</p>

<blockquote>
<pre><i>command1</i> <tt>&amp;</tt> <b>[</b><i>command2</i> <tt>&amp; ...</tt> <b>]</b>
</pre>
</blockquote>

<p>The standard input for an asynchronous list, before any explicit redirections are performed, shall be considered to be assigned
to a file that has the same properties as <b>/dev/null</b>. If it is an interactive shell, this need not happen. In all cases,
explicit redirection of standard input shall override this activity.</p>

<p>When an element of an asynchronous list (the portion of the list ended by an ampersand, such as <i>command1</i>,
above) is
started by the shell, the process ID of the last command in the
asynchronous list element shall become known in the current shell
execution environment; see <a href="#tag_02_12">Shell Execution Environment</a>. This process ID shall remain known until:</p>

<ol>
<li>
<p>The command terminates and the application waits for the process ID.</p>
</li>

<li>
<p>Another asynchronous list invoked before <tt>"$!"</tt> (corresponding to the previous asynchronous list) is expanded in the
current execution environment.</p>
</li>
</ol>

<p>The implementation need not retain more than the {CHILD_MAX} most recent entries in its list of known process IDs in the current
shell execution environment.</p>

<h5><a name="tag_02_09_03_03"></a>Exit Status</h5>

<p>The exit status of an asynchronous list shall be zero.</p>

<h5><a name="tag_02_09_03_04"></a>Sequential Lists</h5>

<p>Commands that are separated by a semicolon ( <tt>';'</tt> ) shall be executed sequentially.</p>

<p>The format for executing commands sequentially shall be:</p>

<blockquote>
<pre><i>command1</i> <b>[</b><tt>;</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>

<p>Each command shall be expanded and executed in the order specified.</p>

<h5><a name="tag_02_09_03_05"></a>Exit Status</h5>

<p>The exit status of a sequential list shall be the exit status of the last command in the list.</p>

<h5><a name="tag_02_09_03_06"></a>AND Lists</h5>

<p>The control operator <tt>"&amp;&amp;"</tt> denotes an AND list. The format shall be:</p>

<blockquote>
<pre><i>command1</i> <b>[</b> <tt>&amp;&amp;</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>

<p>First <i>command1</i> shall be executed. If its exit status is zero, <i>command2</i> shall be executed, and so on, until a
command has a non-zero exit status or there are no more commands left to execute. The commands are expanded only if they are
executed.</p>

<h5><a name="tag_02_09_03_07"></a>Exit Status</h5>

<p>The exit status of an AND list shall be the exit status of the last command that is executed in the list.</p>

<h5><a name="tag_02_09_03_08"></a>OR Lists</h5>

<p>The control operator <tt>"||"</tt> denotes an OR List. The format shall be:</p>

<blockquote>
<pre><i>command1</i> <b>[</b> <tt>||</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>

<p>First, <i>command1</i> shall be executed. If its exit status is non-zero, <i>command2</i> shall be executed, and so on, until a
command has a zero exit status or there are no more commands left to execute.</p>

<h5><a name="tag_02_09_03_09"></a>Exit Status</h5>

<p>The exit status of an OR list shall be the exit status of the last command that is executed in the list.</p>

<h4><a name="tag_02_09_04">  2.9.4 </a>Compound Commands</h4>

<p>The shell has several programming constructs that are "compound
commands", which provide control flow for commands. Each of
these compound commands has a reserved word or control operator at the
beginning, and a corresponding terminator reserved word or
operator at the end. In addition, each can be followed by redirections
on the same line as the terminator. Each redirection shall
apply to all the commands within the compound command that do not
explicitly override that redirection.</p>

<h5><a name="tag_02_09_04_01"></a>Grouping Commands</h5>

<p>The format for grouping commands is as follows:</p>

<dl compact="compact">