Skip to content
xcu_chap02.html 127 KiB
Newer Older
Seblu's avatar
Seblu committed
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 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
<dt>(<i>compound-list</i>)</dt>

<dd>Execute <i>compound-list</i> in a subshell environment; see <a href="#tag_02_12">Shell Execution Environment</a>. Variable
assignments and built-in commands that affect the environment shall not remain in effect after the list finishes.</dd>

<dt>{&nbsp;<i>compound-list</i>;}</dt>

<dd>Execute <i>compound-list</i> in the current process environment. The semicolon shown here is an example of a control operator
delimiting the <b>}</b> reserved word. Other delimiters are possible, as shown in <a href="#tag_02_10">Shell Grammar</a>; a
&lt;newline&gt; is frequently used.</dd>
</dl>

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

<p>The exit status of a grouping command shall be the exit status of <i>compound-list</i>.</p>

<h5><a name="tag_02_09_04_03"></a>The for Loop</h5>

<p>The <b>for</b> loop shall execute a sequence of commands for each member in a list of <i>items</i>. The <b>for</b> loop requires
that the reserved words <b>do</b> and <b>done</b> be used to delimit the sequence of commands.</p>

<p>The format for the <b>for</b> loop is as follows:</p>

<blockquote>
<pre><tt>for</tt> <i>name</i> <tt></tt><b>[</b> <tt>in</tt> <b>[</b><i>word</i> <tt>...</tt> <b>]]</b><tt>do
   </tt> <i>compound-list</i><tt>
<br>
done
</tt>
</pre>
</blockquote>

<p>First, the list of words following <b>in</b> shall be expanded to generate a list of items. Then, the variable <i>name</i> shall
be set to each item, in turn, and the <i>compound-list</i> executed each time. If no items result from the expansion, the
<i>compound-list</i> shall not be executed. Omitting:</p>

<blockquote>
<pre><tt>in</tt> <i>word</i> ...
</pre>
</blockquote>

<p>shall be equivalent to:</p>

<blockquote>
<pre><tt>in "$@"
</tt>
</pre>
</blockquote>

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

<p>The exit status of a <b>for</b> command shall be the exit status of the last command that executes. If there are no items, the
exit status shall be zero.</p>

<h5><a name="tag_02_09_04_05"></a>Case Conditional Construct</h5>

<p>The conditional construct <b>case</b> shall execute the <i>compound-list</i> corresponding to the first one of several
<i>patterns</i> (see <a href="#tag_02_13">Pattern Matching Notation</a>)
that is matched by the string resulting from the tilde
expansion, parameter expansion, command substitution, arithmetic
expansion, and quote removal of the given word. The reserved word
<b>in</b> shall denote the beginning of the patterns to be matched. Multiple patterns with the same <i>compound-list</i> shall be
delimited by the <tt>'|'</tt> symbol. The control operator <tt>')'</tt> terminates a list of patterns corresponding to a given
action. The <i>compound-list</i> for each list of patterns, with the possible exception of the last, shall be terminated with
<tt>";;"</tt>. The <b>case</b> construct terminates with the reserved word <b>esac</b> ( <b>case</b> reversed).</p>

<p>The format for the <b>case</b> construct is as follows:</p>

<blockquote>
<pre><tt>case</tt> <i>word</i> <tt>in
   </tt> <b>[</b><tt>(</tt><b>]</b><i>pattern1</i><tt>)</tt> <i>compound-list</i><tt>;;
   </tt> <b>[[</b><tt>(</tt><b>]</b><i>pattern</i><b>[</b> <tt>|</tt> <i>pattern</i><b>]</b> <tt>... )</tt> <i>compound-list</i><tt>;;</tt><b>]</b> <tt>...
   </tt> <b>[[</b><tt>(</tt><b>]</b><i>pattern</i><b>[</b> <tt>|</tt> <i>pattern</i><b>]</b> <tt>... )</tt> <i>compound-list</i><b>]</b><tt>
<br>
esac
</tt>
</pre>
</blockquote>

<p>The <tt>";;"</tt> is optional for the last <i>compound-list</i>.</p>

<p>In order from the beginning to the end of the <b>case</b> statement, each <i>pattern</i> that labels a <i>compound-list</i>shall
be subjected to tilde expansion, parameter expansion, command
substitution, and arithmetic expansion, and the result of these
expansions shall be compared against the expansion of <i>word</i>, according to the rules described in <a href="#tag_02_13">Pattern
Matching Notation</a> (which also describes the effect of quoting parts of the pattern). After the first match, no more patterns
shall be expanded, and the <i>compound-list</i> shall be executed. The order of expansion and comparison of multiple
<i>pattern</i>s that label a <i>compound-list</i> statement is unspecified.</p>

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

<p>The exit status of <b>case</b> shall be zero if no patterns are matched. Otherwise, the exit status shall be the exit status of
the last command executed in the <i>compound-list</i>.</p>

<h5><a name="tag_02_09_04_07"></a>The if Conditional Construct</h5>

<p>The <b>if</b> command shall execute a <i>compound-list</i> and use its exit status to determine whether to execute another
<i>compound-list</i>.</p>

<p>The format for the <b>if</b> construct is as follows:</p>

<blockquote>
<pre><tt>if</tt> <i>compound-list</i><tt>then
   </tt> <i>compound-list</i><b>[</b><tt>elif</tt> <i>compound-list</i><tt>then
   </tt> <i>compound-list</i><b>]</b> <tt>...
</tt><b>[</b><tt>else
   </tt> <i>compound-list</i><b>]</b><tt>
<br>
fi
</tt>
</pre>
</blockquote>

<p>The <b>if</b> <i>compound-list</i> shall be executed; if its exit status is zero, the <b>then</b> <i>compound-list</i> shall be
executed and the command shall complete. Otherwise, each <b>elif</b> <i>compound-list</i> shall be executed, in turn, and if its
exit status is zero, the <b>then</b> <i>compound-list</i> shall be executed and the command shall complete. Otherwise, the
<b>else</b> <i>compound-list</i> shall be executed.</p>

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

<p>The exit status of the <b>if</b> command shall be the exit status of the <b>then</b> or <b>else</b> <i>compound-list</i> that
was executed, or zero, if none was executed.</p>

<h5><a name="tag_02_09_04_09"></a>The while Loop</h5>

<p>The <b>while</b> loop shall continuously execute one <i>compound-list</i> as long as another <i>compound-list</i> has a zero
exit status.</p>

<p>The format of the <b>while</b> loop is as follows:</p>

<blockquote>
<pre><tt>while</tt> <i>compound-list-1</i><tt>do
   </tt> <i>compound-list-2</i><tt>
<br>
done
</tt>
</pre>
</blockquote>

<p>The <i>compound-list-1</i> shall be executed, and if it has a non-zero exit status, the <b>while</b> command shall complete.
Otherwise, the <i>compound-list-2</i> shall be executed, and the process shall repeat.</p>

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

<p>The exit status of the <b>while</b> loop shall be the exit status of the last <i>compound-list-2</i> executed, or zero if none
was executed.</p>

<h5><a name="tag_02_09_04_11"></a>The until Loop</h5>

<p>The <b>until</b> loop shall continuously execute one <i>compound-list</i> as long as another <i>compound-list</i> has a non-zero
exit status.</p>

<p>The format of the <b>until</b> loop is as follows:</p>

<blockquote>
<pre><tt>until</tt> <i>compound-list-1</i><tt>do
   </tt> <i>compound-list-2</i><tt>
<br>
done
</tt>
</pre>
</blockquote>

<p>The <i>compound-list-1</i> shall be executed, and if it has a zero exit status, the <b>until</b> command completes. Otherwise,
the <i>compound-list-2</i> shall be executed, and the process repeats.</p>

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

<p>The exit status of the <b>until</b> loop shall be the exit status of the last <i>compound-list-2</i> executed, or zero if none
was executed.</p>

<h4><a name="tag_02_09_05">  2.9.5 </a>Function Definition Command</h4>

<p>A function is a user-defined name that is used as a simple command to call a compound command with new positional parameters. A
function is defined with a "function definition command".</p>

<p>The format of a function definition command is as follows:</p>

<blockquote>
<pre><i>fname</i><tt>()</tt> <i>compound-command</i><b>[</b><i>io-redirect</i> <tt>...</tt><b>]</b>
</pre>
</blockquote>

<p>The function is named <i>fname</i>; the application shall ensure that it is a name (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_230">Section 3.230, Name</a>).
An implementation may
allow other characters in a function name as an extension. The
implementation shall maintain separate name spaces for functions and
variables.</p>

<p>The argument <i>compound-command</i> represents a compound command, as described in <a href="#tag_02_09_04">Compound
Commands</a>.</p>

<p>When the function is declared, none of the expansions in <a href="#tag_02_06">Word Expansions</a> shall be performed on the text
in <i>compound-command</i> or <i>io-redirect</i>; all expansions shall be performed as normal each time the function is called.
Similarly, the optional <i>io-redirect</i> redirections and any variable assignments within <i>compound-command</i> shall be
performed during the execution of the function itself, not the function definition. See <a href="#tag_02_08_01">Consequences of
Shell Errors</a> for the consequences of failures of these operations on interactive and non-interactive shells.</p>

<p>When a function is executed, it shall have the syntax-error and variable-assignment properties described for special built-in
utilities in the enumerated list at the beginning of <a href="#tag_02_14">Special Built-In Utilities</a>.</p>

<p>The <i>compound-command</i> shall be executed whenever the function name is specified as the name of a simple command (see <a href="#tag_02_09_01_01">Command Search and Execution</a>). The operands to the command temporarily shall become the positional
parameters during the execution of the <i>compound-command</i>; the special parameter <tt>'#'</tt> also shall be changed to reflect
the number of operands. The special parameter 0 shall be unchanged. When the function completes, the values of the positional
parameters and the special parameter <tt>'#'</tt> shall be restored to the values they had before the function was executed. If the
special built-in <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/return.html"><i>return</i></a> is executed in the <i>compound-command</i>, the function completes and
execution shall resume with the next command after the function call.</p>

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

<p>The exit status of a function definition shall be zero if the function was declared successfully; otherwise, it shall be greater
than zero. The exit status of a function invocation shall be the exit status of the last command executed by the function.</p>

<h3><a name="tag_02_10">   2.10 </a>Shell Grammar</h3>

<p>The following grammar defines the Shell Command Language. This formal syntax shall take precedence over the preceding text
syntax description.</p>

<h4><a name="tag_02_10_01">  2.10.1 </a>Shell Grammar Lexical Conventions</h4>

<p>The input language to the shell must be first recognized at the character level. The resulting tokens shall be classified by
their immediate context according to the following rules (applied in order). These rules shall be used to determine what a
"token" is that is subject to parsing at the token level. The rules for token recognition in <a href="#tag_02_03">Token
Recognition</a> shall apply.</p>

<ol>
<li>
<p>A &lt;newline&gt; shall be returned as the token identifier <b>NEWLINE</b>.</p>
</li>

<li>
<p>If the token is an operator, the token identifier for that operator shall result.</p>
</li>

<li>
<p>If the string consists solely of digits and the delimiter character is one of <tt>'&lt;'</tt> or <tt>'&gt;'</tt>, the token
identifier <b>IO_NUMBER</b> shall be returned.</p>
</li>

<li>
<p>Otherwise, the token identifier <b>TOKEN</b> results.</p>
</li>
</ol>

<p>Further distinction on <b>TOKEN</b> is context-dependent. It may be that the same <b>TOKEN</b> yields <b>WORD</b>, a
<b>NAME</b>, an <b>ASSIGNMENT</b>, or one of the reserved words below, dependent upon the context. Some of the productions in the
grammar below are annotated with a rule number from the following list. When a <b>TOKEN</b> is seen where one of those annotated
productions could be used to reduce the symbol, the applicable rule shall be applied to convert the token identifier type of the
<b>TOKEN</b> to a token identifier acceptable at that point in the
grammar. The reduction shall then proceed based upon the token
identifier type yielded by the rule applied. When more than one rule
applies, the highest numbered rule shall apply (which in turn
may refer to another rule). (Note that except in rule 7, the presence
of an <tt>'='</tt> in the token has no effect.)</p>

<p>The <b>WORD</b> tokens shall have the word expansion rules applied to them immediately before the associated command is
executed, not at the time the command is parsed.</p>

<h4><a name="tag_02_10_02">  2.10.2 </a>Shell Grammar Rules</h4>

<ol>
<li>
<p>[Command Name]</p>

<p>When the <b>TOKEN</b> is exactly a reserved word, the token identifier for that reserved word shall result. Otherwise, the token
<b>WORD</b> shall be returned. Also, if the parser is in any state where only a reserved word could be the next correct token,
proceed as above. <basefont size="2"></p>

<dl>
<dt><b>Note:</b></dt>

<dd>Because at this point quote marks are retained in the token, quoted
strings cannot be recognized as reserved words. This rule
also implies that reserved words are not recognized except in certain
positions in the input, such as after a &lt;newline&gt; or
semicolon; the grammar presumes that if the reserved word is intended,
it is properly delimited by the user, and does not attempt
to reflect that requirement directly. Also note that line joining is
done before tokenization, as described in <a href="#tag_02_02_01">Escape Character (Backslash)</a> , so escaped &lt;newline&gt;s are already removed at this point.</dd>
</dl>

<basefont size="3"> 

<p>Rule 1 is not directly referenced in the grammar, but is referred to by other rules, or applies globally.</p>
</li>

<li>
<p>[Redirection to or from filename]</p>

<p>The expansions specified in <a href="#tag_02_07">Redirection</a> shall occur. As specified there, exactly one field can result
(or the result is unspecified), and there are additional requirements on pathname expansion.</p>
</li>

<li>
<p>[Redirection from here-document]</p>

<p>Quote removal shall be applied to the word to determine the delimiter that is used to find the end of the here-document that
begins after the next &lt;newline&gt;.</p>
</li>

<li>
<p>[Case statement termination]</p>

<p>When the <b>TOKEN</b> is exactly the reserved word <b>esac</b>, the token identifier for <b>esac</b> shall result. Otherwise,
the token <b>WORD</b> shall be returned.</p>
</li>

<li>
<p>[ <b>NAME</b> in <b>for</b>]</p>

<p>When the <b>TOKEN</b> meets the requirements for a name (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_230">Section 3.230, Name</a>), the token identifier <b>NAME</b> shall result. Otherwise,
the token <b>WORD</b> shall be returned.</p>
</li>

<li>
<p>[Third word of <b>for</b> and <b>case</b>]</p>

<ol type="a">
<li>
<p>[ <b>case</b> only]</p>

<p>When the <b>TOKEN</b> is exactly the reserved word <b>in</b>, the token identifier for <b>in</b> shall result. Otherwise, the
token <b>WORD</b> shall be returned.</p>
</li>

<li>
<p>[ <b>for</b> only]</p>

<p>When the <b>TOKEN</b> is exactly the reserved word <b>in</b> or <b>do</b>, the token identifier for <b>in</b> or <b>do</b> shall
result, respectively. Otherwise, the token <b>WORD</b> shall be returned.</p>
</li>
</ol>

<p>(For a. and b.: As indicated in the grammar, a <i>linebreak</i> precedes the tokens <b>in</b> and <b>do</b>. If &lt;newline&gt;s
are present at the indicated location, it is the token after them that is treated in this fashion.)</p>
</li>

<li>
<p>[Assignment preceding command name]</p>

<ol type="a">
<li>
<p>[When the first word]</p>

<p>If the <b>TOKEN</b> does not contain the character <tt>'='</tt>, rule 1 is applied. Otherwise, 7b shall be applied.</p>
</li>

<li>
<p>[Not the first word]</p>

<p>If the <b>TOKEN</b> contains the equal sign character:</p>

<ul>
<li>
<p>If it begins with <tt>'='</tt>, the token <b>WORD</b> shall be returned.</p>
</li>

<li>
<p>If all the characters preceding <tt>'='</tt> form a valid name (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_230">Section 3.230, Name</a>), the token
<b>ASSIGNMENT_WORD</b> shall be returned. (Quoted characters cannot participate in forming a valid name.)</p>
</li>

<li>
<p>Otherwise, it is unspecified whether it is <b>ASSIGNMENT_WORD</b> or <b>WORD</b> that is returned.</p>
</li>
</ul>
</li>
</ol>

<p>Assignment to the <b>NAME</b> shall occur as specified in <a href="#tag_02_09_01">Simple Commands</a>.</p>
</li>

<li>
<p>[ <b>NAME</b> in function]</p>

<p>When the <b>TOKEN</b> is exactly a reserved word, the token identifier for that reserved word shall result. Otherwise, when the
<b>TOKEN</b> meets the requirements for a name, the token identifier <b>NAME</b> shall result. Otherwise, rule 7 applies.</p>
</li>

<li>
<p>[Body of function]</p>

<p>Word expansion and assignment shall never occur, even when required by the rules above, when this rule is being parsed. Each
<b>TOKEN</b> that might either be expanded or have assignment applied to it shall instead be returned as a single <b>WORD</b>
consisting only of characters that are exactly the token described in <a href="#tag_02_03">Token Recognition</a>.</p>
</li>
</ol>

<br>
<pre><tt>/* -------------------------------------------------------
   The grammar symbols
   ------------------------------------------------------- */
<br>
%token  WORD
%token  ASSIGNMENT_WORD
%token  NAME
%token  NEWLINE
%token  IO_NUMBER
<br>
/* The following are the operators mentioned above. */
<br>
%token  AND_IF    OR_IF    DSEMI
/*      '&amp;&amp;'      '||'     ';;'    */
<br>
%token  DLESS  DGREAT  LESSAND  GREATAND  LESSGREAT  DLESSDASH
/*      '&lt;&lt;'   '&gt;&gt;'    '&lt;&amp;'     '&gt;&amp;'      '&lt;&gt;'       '&lt;&lt;-'   */
<br>
%token  CLOBBER
/*      '&gt;|'   */
<br>
/* The following are the reserved words. */
<br>
%token  If    Then    Else    Elif    Fi    Do    Done
/*      'if'  'then'  'else'  'elif'  'fi'  'do'  'done'   */
<br>
%token  Case    Esac    While    Until    For
/*      'case'  'esac'  'while'  'until'  'for'   */
<br>
/* These are reserved words, not operator tokens, and are
   recognized when reserved words are recognized. */
<br>
%token  Lbrace    Rbrace    Bang
/*      '{'       '}'       '!'   */
<br>
%token  In
/*      'in'   */
<br>
/* -------------------------------------------------------
   The Grammar
   ------------------------------------------------------- */
<br>
%start  complete_command
%%
complete_command : list separator
                 | list
                 ;
list             : list separator_op and_or
                 |                   and_or
                 ;
and_or           :                         pipeline
                 | and_or AND_IF linebreak pipeline
                 | and_or OR_IF  linebreak pipeline
                 ;
pipeline         :      pipe_sequence
                 | Bang pipe_sequence
                 ;
pipe_sequence    :                             command
                 | pipe_sequence '|' linebreak command
                 ;
command          : simple_command
                 | compound_command
                 | compound_command redirect_list
                 | function_definition
                 ;
compound_command : brace_group
                 | subshell
                 | for_clause
                 | case_clause
                 | if_clause
                 | while_clause
                 | until_clause
                 ;
subshell         : '(' compound_list ')'
                 ;
compound_list    :              term
                 | newline_list term
                 |              term separator
                 | newline_list term separator
                 ;
term             : term separator and_or
                 |                and_or
                 ;
for_clause       : For name linebreak                            do_group
                 | For name linebreak in          sequential_sep do_group
                 | For name linebreak in wordlist sequential_sep do_group
                 ;
name             : NAME                     /* Apply rule 5 */
                 ;
in               : In                       /* Apply rule 6 */
                 ;
wordlist         : wordlist WORD
                 |          WORD
                 ;
case_clause      : Case WORD linebreak in linebreak case_list    Esac
                 | Case WORD linebreak in linebreak case_list_ns Esac
                 | Case WORD linebreak in linebreak              Esac
                 ;
case_list_ns     : case_list case_item_ns
                 |           case_item_ns
                 ;
case_list        : case_list case_item
                 |           case_item
                 ;
case_item_ns     :     pattern ')'               linebreak
                 |     pattern ')' compound_list linebreak
                 | '(' pattern ')'               linebreak
                 | '(' pattern ')' compound_list linebreak
                 ;
case_item        :     pattern ')' linebreak     DSEMI linebreak
                 |     pattern ')' compound_list DSEMI linebreak
                 | '(' pattern ')' linebreak     DSEMI linebreak
                 | '(' pattern ')' compound_list DSEMI linebreak
                 ;
pattern          :             WORD         /* Apply rule 4 */
                 | pattern '|' WORD         /* Do not apply rule 4 */
                 ;
if_clause        : If compound_list Then compound_list else_part Fi
                 | If compound_list Then compound_list           Fi
                 ;
else_part        : Elif compound_list Then else_part
                 | Else compound_list
                 ;
while_clause     : While compound_list do_group
                 ;
until_clause     : Until compound_list do_group
                 ;
function_definition : fname '(' ')' linebreak function_body
                 ;
function_body    : compound_command                /* Apply rule 9 */
                 | compound_command redirect_list  /* Apply rule 9 */
                 ;
fname            : NAME                            /* Apply rule 8 */
                 ;
brace_group      : Lbrace compound_list Rbrace
                 ;
do_group         : Do compound_list Done           /* Apply rule 6 */
                 ;
simple_command   : cmd_prefix cmd_word cmd_suffix
                 | cmd_prefix cmd_word
                 | cmd_prefix
                 | cmd_name cmd_suffix
                 | cmd_name
                 ;
cmd_name         : WORD                   /* Apply rule 7a */
                 ;
cmd_word         : WORD                   /* Apply rule 7b */
                 ;
cmd_prefix       :            io_redirect
                 | cmd_prefix io_redirect
                 |            ASSIGNMENT_WORD
                 | cmd_prefix ASSIGNMENT_WORD
                 ;
cmd_suffix       :            io_redirect
                 | cmd_suffix io_redirect
                 |            WORD
                 | cmd_suffix WORD
                 ;
redirect_list    :               io_redirect
                 | redirect_list io_redirect
                 ;
io_redirect      :           io_file
                 | IO_NUMBER io_file
                 |           io_here
                 | IO_NUMBER io_here
                 ;
io_file          : '&lt;'       filename
                 | LESSAND   filename
                 | '&gt;'       filename
                 | GREATAND  filename
                 | DGREAT    filename
                 | LESSGREAT filename
                 | CLOBBER   filename
                 ;
filename         : WORD                      /* Apply rule 2 */
                 ;
io_here          : DLESS     here_end
                 | DLESSDASH here_end
                 ;
here_end         : WORD                      /* Apply rule 3 */
                 ;
newline_list     :              NEWLINE
                 | newline_list NEWLINE
                 ;
linebreak        : newline_list
                 | /* empty */
                 ;
separator_op     : '&amp;'
                 | ';'
                 ;
separator        : separator_op linebreak
                 | newline_list
                 ;
sequential_sep   : ';' linebreak
                 | newline_list
                 ;
</tt>
</pre>

<h3><a name="tag_02_11">   2.11 </a>Signals and Error Handling</h3>

<p>When a command is in an asynchronous list, the shell shall prevent SIGQUIT and SIGINT signals from the keyboard from
interrupting the command. Otherwise, signals shall have the values inherited by the shell from its parent (see also the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html#tag_04_146"><i>trap</i></a> special built-in).</p>

<p>When a signal for which a trap has been set is received while the shell is waiting for the completion of a utility executing a
foreground command, the trap associated with that signal shall not be executed until after the foreground command has completed.
When the shell is waiting, by means of the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/wait.html"><i>wait</i></a> utility, for asynchronous commands to
complete, the reception of a signal for which a trap has been set shall cause the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/wait.html"><i>wait</i></a>
utility to return immediately with an exit status &gt;128, immediately after which the trap associated with that signal shall be
taken.</p>

<p>If multiple signals are pending for the shell for which there are associated trap actions, the order of execution of trap
actions is unspecified.</p>

<h3><a name="tag_02_12">   2.12 </a>Shell Execution Environment</h3>

<p>A shell execution environment consists of the following:</p>

<ul>
<li>
<p>Open files inherited upon invocation of the shell, plus open files controlled by <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/exec.html"><i>exec</i></a></p>
</li>

<li>
<p>Working directory as set by <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/cd.html"><i>cd</i></a></p>
</li>

<li>
<p>File creation mask set by <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/umask.html"><i>umask</i></a></p>
</li>

<li>
<p>Current traps set by <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html"><i>trap</i></a></p>
</li>

<li>
<p>Shell parameters that are set by variable assignment (see the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/set.html#tag_04_127"><i>set</i></a> special built-in) or
from the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 environment inherited by the shell when it begins (see the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/export.html#tag_04_49"><i>export</i></a> special built-in)</p>
</li>

<li>
<p>Shell functions; see <a href="#tag_02_09_05">Function Definition Command</a></p>
</li>

<li>
<p>Options turned on at invocation or by <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/set.html"><i>set</i></a></p>
</li>

<li>
<p>Process IDs of the last commands in asynchronous lists known to this shell environment; see <a href="#tag_02_09_03_02">Asynchronous Lists</a></p>
</li>

<li>
<p>Shell aliases; see <a href="#tag_02_03_01">Alias Substitution</a></p>
</li>
</ul>

<p>Utilities other than the special built-ins (see <a href="#tag_02_14">Special Built-In Utilities</a>) shall be invoked in a
separate environment that consists of the following. The initial value of these objects shall be the same as that for the parent
shell, except as noted below.</p>

<ul>
<li>
<p>Open files inherited on invocation of the shell, open files controlled by the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/exec.html"><i>exec</i></a> special built-in
plus any modifications, and additions specified by any redirections to the utility</p>
</li>

<li>
<p>Current working directory</p>
</li>

<li>
<p>File creation mask</p>
</li>

<li>
<p>If the utility is a shell script, traps caught by the shell shall be set to the default values and traps ignored by the shell
shall be set to be ignored by the utility; if the utility is not a shell script, the trap actions (default or ignore) shall be
mapped into the appropriate signal handling actions for the utility</p>
</li>

<li>
<p>Variables with the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/export.html"><i>export</i></a> attribute, along with those explicitly exported for the duration of the
command, shall be passed to the utility environment variables</p>
</li>
</ul>

<p>The environment of the shell process shall not be changed by the utility unless explicitly specified by the utility description
(for example, <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/cd.html"><i>cd</i></a> and <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/umask.html"><i>umask</i></a>).</p>

<p>A subshell environment shall be created as a duplicate of the shell
environment, except that signal traps set by that shell
environment shall be set to the default values. Changes made to the
subshell environment shall not affect the shell environment.
Command substitution, commands that are grouped with parentheses, and
asynchronous lists shall be executed in a subshell
environment. Additionally, each command of a multi-command pipeline is
in a subshell environment; as an extension, however, any or
all commands in a pipeline may be executed in the current environment.
All other commands shall be executed in the current shell
environment.</p>

<h3><a name="tag_02_13">   2.13 </a>Pattern Matching Notation</h3>

<p>The pattern matching notation described in this section is used to specify patterns for matching strings in the shell.
Historically, pattern matching notation is related to, but slightly different from, the regular expression notation described in
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html">Chapter 9, Regular
Expressions</a>. For this reason, the description of the rules for this pattern matching notation are based on the description of
regular expression notation, modified to include backslash escape processing.</p>

<h4><a name="tag_02_13_01">  2.13.1 </a>Patterns Matching a Single Character</h4>

<p>The following patterns matching a single character shall match a single character: ordinary characters, special pattern
characters, and pattern bracket expressions. The pattern bracket expression also shall match a single collating element. A
backslash character shall escape the following character. The escaping backslash shall be discarded.</p>

<p>An ordinary character is a pattern that shall match itself. It can be any character in the supported character set except for
NUL, those special shell characters in <a href="#tag_02_02">Quoting</a>
that require quoting, and the following three special
pattern characters. Matching shall be based on the bit pattern used for
encoding the character, not on the graphic representation
of the character. If any character (ordinary, shell special, or pattern
special) is quoted, that pattern shall match the character
itself. The shell special characters always require quoting.</p>

<p>When unquoted and outside a bracket expression, the following three characters shall have special meaning in the specification
of patterns:</p>

<dl compact="compact">
<dt><tt>?</tt></dt>

<dd>A question-mark is a pattern that shall match any character.</dd>

<dt><tt>*</tt></dt>

<dd>An asterisk is a pattern that shall match multiple characters, as described in <a href="#tag_02_13_02">Patterns Matching
Multiple Characters</a>.</dd>

<dt><tt>[</tt></dt>

<dd>The open bracket shall introduce a pattern bracket expression.</dd>
</dl>

<p>The description of basic regular expression bracket expressions in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_03_05">Section 9.3.5, RE Bracket Expression</a> shall
also apply to the pattern bracket expression, except that the exclamation mark character ( <tt>'!'</tt> ) shall replace the
circumflex character ( <tt>'^'</tt> ) in its role in a "non-matching list" in the regular expression notation. A bracket
expression starting with an unquoted circumflex character produces unspecified results.</p>

<p>When pattern matching is used where shell quote removal is not performed (such as in the argument to the <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/find.html"><i>find</i></a> - <i>name</i> primary when <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/find.html"><i>find</i></a> is being
called using one of the <i>exec</i> functions as defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001, or in
the <i>pattern</i> argument to the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fnmatch.html"><i>fnmatch</i>()</a> function), special characters can be
escaped to remove their special meaning by preceding them with a backslash character. This escaping backslash is discarded. The
sequence <tt>"\\"</tt> represents one literal backslash. All of the requirements and effects of quoting on ordinary, shell special,
and special pattern characters shall apply to escaping in this context.</p>

<h4><a name="tag_02_13_02">  2.13.2 </a>Patterns Matching Multiple Characters</h4>

<p>The following rules are used to construct patterns matching multiple characters from patterns matching a single character:</p>

<ol>
<li>
<p>The asterisk ( <tt>'*'</tt> ) is a pattern that shall match any string, including the null string.</p>
</li>

<li>
<p>The concatenation of patterns matching a single character is a valid pattern that shall match the concatenation of the single
characters or collating elements matched by each of the concatenated patterns.</p>
</li>

<li>
<p>The concatenation of one or more patterns matching a single
character with one or more asterisks is a valid pattern. In such
patterns, each asterisk shall match a string of zero or more
characters, matching the greatest possible number of characters that
still allows the remainder of the pattern to match the string.</p>
</li>
</ol>

<h4><a name="tag_02_13_03">  2.13.3 </a>Patterns Used for Filename Expansion</h4>

<p>The rules described so far in <a href="#tag_02_13_01">Patterns Matching a Single Character</a> and <a href="#tag_02_13_02">Patterns Matching Multiple Characters</a> are qualified by the following rules that apply when pattern matching
notation is used for filename expansion:</p>

<ol>
<li>
<p>The slash character in a pathname shall be explicitly matched by
using one or more slashes in the pattern; it shall neither be
matched by the asterisk or question-mark special characters nor by a
bracket expression. Slashes in the pattern shall be identified
before bracket expressions; thus, a slash cannot be included in a
pattern bracket expression used for filename expansion. If a
slash character is found following an unescaped open square bracket
character before a corresponding closing square bracket is
found, the open bracket shall be treated as an ordinary character. For
example, the pattern <tt>"a[b/c]d"</tt> does not match such
pathnames as <b>abd</b> or <b>a/d</b>. It only matches a pathname of literally <b>a[b/c]d</b>.</p>
</li>

<li>
<p>If a filename begins with a period ( <tt>'.'</tt> ), the period shall be explicitly matched by using a period as the first
character of the pattern or immediately following a slash character. The leading period shall not be matched by:</p>

<ul>
<li>
<p>The asterisk or question-mark special characters</p>
</li>

<li>
<p>A bracket expression containing a non-matching list, such as <tt>"[!a]"</tt>, a range expression, such as <tt>"[%-0]"</tt>, or
a character class expression, such as <tt>"[[:punct:]]"</tt></p>
</li>
</ul>

<p>It is unspecified whether an explicit period in a bracket expression matching list, such as <tt>"[.abc]"</tt>, can match a
leading period in a filename.</p>
</li>

<li>
<p>Specified patterns shall be matched against existing filenames and pathnames, as appropriate. Each component that contains a
pattern character shall require read permission in the directory containing that component. Any component, except the last, that
does not contain a pattern character shall require search permission. For example, given the pattern:</p>

<blockquote>
<pre><tt>/foo/bar/x*/bam
</tt>
</pre>
</blockquote>

<p>search permission is needed for directories <b>/</b> and <b>foo</b>, search and read permissions are needed for directory
<b>bar</b>, and search permission is needed for each <b>x*</b>
directory. If the pattern matches any existing filenames or
pathnames, the pattern shall be replaced with those filenames and
pathnames, sorted according to the collating sequence in effect
in the current locale. If the pattern contains an invalid bracket
expression or does not match any existing filenames or pathnames,
the pattern string shall be left unchanged.</p>
</li>
</ol>

<h3><a name="tag_02_14">   2.14 </a>Special Built-In Utilities</h3>

<p>The following "<a href="http://www.opengroup.org/onlinepubs/009695399/idx/sbi.html">special built-in</a>" utilities shall be supported in the shell command language. The output of each command, if
any, shall be written to standard output, subject to the normal redirection and piping possible with all commands.</p>

<p>The term "built-in" implies that the shell can execute the utility directly and does not need to search for it. An
implementation may choose to make any utility a built-in; however, the special built-in utilities described here differ from
regular built-in utilities in two respects:</p>

<ol>
<li>
<p>A syntax error in a special built-in utility may cause a shell executing that utility to abort, while a syntax error in a
regular built-in utility shall not cause a shell executing that utility to abort. (See <a href="#tag_02_08_01">Consequences of
Shell Errors</a> for the consequences of errors on interactive and non-interactive shells.) If a special built-in utility
encountering a syntax error does not abort the shell, its exit value shall be non-zero.</p>
</li>

<li>
<p>Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be
the case with a regular built-in or other utility.</p>
</li>
</ol>

<p>The special built-in utilities in this section need not be provided in a manner accessible via the <i>exec</i> family of
functions defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001.</p>

<p>Some of the special built-ins are described as conforming to the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>. For those that are not, the requirement
in <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap01.html#tag_01_11"><i>Utility Description Defaults</i></a> that <tt>"--"</tt> be recognized as a first argument
to be discarded does not apply and a conforming application shall not use that argument.</p>

<hr noshade="noshade" size="2">
<center><font size="2"><!--footer start-->
UNIX ® is a registered Trademark of The Open Group.<br>
POSIX ® is a registered Trademark of The IEEE.<br>
[ <a href="http://www.opengroup.org/onlinepubs/009695399/mindex.html">Main Index</a> | <a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/contents.html">XBD</a> | <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/contents.html">XCU</a> | <a href="http://www.opengroup.org/onlinepubs/009695399/functions/contents.html">XSH</a> | <a href="http://www.opengroup.org/onlinepubs/009695399/xrat/contents.html">XRAT</a>
]</font></center>

<!--footer end-->
<hr noshade="noshade" size="2">

</body></html>