/usr/share/doc/zsh-common/html/Shell-Grammar.html is in zsh-doc 5.4.2-3ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>6 Shell Grammar (zsh)</title>
<meta name="description" content="6 Shell Grammar (zsh)">
<meta name="keywords" content="6 Shell Grammar (zsh)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2any">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Shell-Grammar"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="Files.html#Files" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="Files.html#Files-2" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Simple-Commands-_0026-Pipelines" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Shell-Grammar-1"></a>
<h1 class="chapter">6 Shell Grammar</h1>
<p><a name="index-shell-grammar"></a>
<a name="index-grammar_002c-shell"></a>
</p><table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Simple-Commands-_0026-Pipelines">6.1 Simple Commands & Pipelines</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Precommand-Modifiers">6.2 Precommand Modifiers</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Complex-Commands">6.3 Complex Commands</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Alternate-Forms-For-Complex-Commands">6.4 Alternate Forms For Complex Commands</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Reserved-Words">6.5 Reserved Words</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Errors">6.6 Errors</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Comments">6.7 Comments</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Aliasing">6.8 Aliasing</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Quoting">6.9 Quoting</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Simple-Commands-_0026-Pipelines"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Precommand-Modifiers" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Simple-Commands-_0026-Pipelines-1"></a>
<h2 class="section">6.1 Simple Commands & Pipelines</h2>
<p><a name="index-simple-commands"></a>
<a name="index-commands_002c-simple"></a>
A <em>simple command</em> is a sequence of optional parameter
assignments followed by blank-separated words,
with optional redirections interspersed. For a description
of assignment, see the beginning of
<a href="Parameters.html#Parameters">Parameters</a>.
</p>
<p>The first word is the command to be executed, and the remaining
words, if any, are arguments to the command.
If a command name is given, the parameter assignments modify
the environment of the command when it is executed.
The value of a simple command is its exit status,
or 128 plus the signal number if terminated by a signal.
For example,
</p>
<div class="example">
<pre class="example">echo foo
</pre></div>
<p>is a simple command with arguments.
</p>
<p><a name="index-pipeline"></a>
A <em>pipeline</em> is either a simple command, or a sequence of two or more
simple commands where each command is separated from the next by ‘<tt>|</tt>’
or ‘<tt>|&</tt>’. Where commands are separated by ‘<tt>|</tt>’, the standard
output of the first command is connected to the
standard input of the next. ‘<tt>|&</tt>’ is shorthand for ‘<tt>2>&1 |</tt>’, which
connects both the standard output and the standard error of the
command to the standard input of the next. The value of a pipeline
is the value of the last command, unless the pipeline is preceded by
‘<tt>!</tt>’ in which case the value is the logical inverse of the value of the
last command.
For example,
</p>
<div class="example">
<pre class="example">echo foo | sed 's/foo/bar/'
</pre></div>
<p>is a pipeline, where the output (‘<tt>foo</tt>’ plus a newline) of the first
command will be passed to the input of the second.
</p>
<p><a name="index-coproc"></a>
<a name="index-coprocess"></a>
If a pipeline is preceded by ‘<tt>coproc</tt>’, it is executed as a coprocess;
a two-way pipe is established between it and the parent shell. The
shell can read from or write to the coprocess by means of the ‘<tt>>&p</tt>’
and ‘<tt><&p</tt>’ redirection operators or with ‘<tt>print -p</tt>’ and ‘<tt>read -p</tt>’.
A pipeline cannot be preceded by both ‘<tt>coproc</tt>’ and ‘<tt>!</tt>’.
If job control is active, the coprocess can be treated in other than input
and output as an ordinary background job.
</p>
<p><a name="index-sublist"></a>
A <em>sublist</em> is either a single pipeline, or a sequence of two or more
pipelines separated by ‘<tt>&&</tt>’ or ‘<tt>||</tt>’. If two pipelines are separated
by ‘<tt>&&</tt>’, the second pipeline is executed only if the first succeeds
(returns a zero status). If two pipelines are separated by ‘<tt>||</tt>’, the
second is executed only if the first fails (returns a nonzero status).
Both operators have equal precedence and are left associative.
The value of the sublist is the value of the last pipeline executed.
For example,
</p>
<div class="example">
<pre class="example">dmesg | grep panic && print yes
</pre></div>
<p>is a sublist consisting of two pipelines, the second just a simple command
which will be executed if and only if the <tt>grep</tt> command returns a zero
status. If it does not, the value of the sublist is that return status, else
it is the status returned by the <tt>print</tt> (almost certainly zero).
</p>
<p><a name="index-list"></a>
A <em>list</em> is a sequence of zero or more sublists, in which each sublist
is terminated by ‘<tt>;</tt>’, ‘<tt>&</tt>’, ‘<tt>&|</tt>’, ‘<tt>&!</tt>’, or a newline.
This terminator
may optionally be omitted from the last sublist in the list when the
list appears as a complex command inside ‘<tt>(</tt>...<tt>)</tt>’
or ‘<tt>{</tt>...<tt>}</tt>’. When a
sublist is terminated by ‘<tt>;</tt>’ or newline, the shell waits for it to
finish before executing the next sublist. If a sublist is terminated
by a ‘<tt>&</tt>’, ‘<tt>&|</tt>’, or ‘<tt>&!</tt>’,
the shell executes the last pipeline in it in the background, and
does not wait for it to finish (note the difference from other shells
which execute the whole sublist in the background).
A backgrounded pipeline returns a status of zero.
</p>
<p>More generally, a list can be seen as a set of any shell commands
whatsoever, including the complex commands below; this is implied wherever
the word ‘list’ appears in later descriptions. For example, the commands
in a shell function form a special sort of list.
</p><hr>
<a name="Precommand-Modifiers"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Simple-Commands-_0026-Pipelines" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Complex-Commands" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Precommand-Modifiers-1"></a>
<h2 class="section">6.2 Precommand Modifiers</h2>
<p><a name="index-precommand-modifiers"></a>
<a name="index-modifiers_002c-precommand"></a>
A simple command may be preceded by a <em>precommand modifier</em>,
which will alter how the command is interpreted. These modifiers are
shell builtin commands with the exception of <tt>nocorrect</tt> which is
a reserved word.
</p>
<dl compact="compact">
<dd><a name="index-_002d"></a>
</dd>
<dt><tt>-</tt></dt>
<dd><p>The command is executed with a ‘<tt>-</tt>’ prepended to its
<tt>argv[0]</tt> string.
</p>
<a name="index-builtin"></a>
</dd>
<dt><tt>builtin</tt></dt>
<dd><p>The command word is taken to be the name of a builtin command,
rather than a shell function or external command.
</p>
<a name="index-command"></a>
</dd>
<dt><tt>command</tt> [ <tt>-pvV</tt> ]</dt>
<dd><p>The command word is taken to be the name of an external command,
rather than a shell function or builtin. If the <tt>POSIX_BUILTINS</tt> option
is set, builtins will also be executed but certain special properties
of them are suppressed. The <tt>-p</tt> flag causes a default path to be
searched instead of that in <tt>$path</tt>. With the <tt>-v</tt> flag, <tt>command</tt>
is similar to <tt>whence</tt> and with <tt>-V</tt>, it is equivalent to <tt>whence
-v</tt>.
</p>
<a name="index-exec"></a>
</dd>
<dt><tt>exec</tt> [ <tt>-cl</tt> ] [ <tt>-a</tt> <var>argv0</var> ]</dt>
<dd><p>The following command together with any arguments is run in place
of the current process, rather than as a sub-process. The shell does not
fork and is replaced. The shell does not invoke <tt>TRAPEXIT</tt>, nor does it
source <tt>zlogout</tt> files.
The options are provided for compatibility with other shells.
</p>
<p>The <tt>-c</tt> option clears the environment.
</p>
<p>The <tt>-l</tt> option is equivalent to the <tt>-</tt> precommand modifier, to
treat the replacement command as a login shell; the command is executed
with a <tt>-</tt> prepended to its <tt>argv[0]</tt> string. This flag has no effect
if used together with the <tt>-a</tt> option.
</p>
<p>The <tt>-a</tt> option is used to specify explicitly the <tt>argv[0]</tt> string
(the name of the command as seen by the process itself) to be used by the
replacement command and is directly equivalent to setting a value
for the <tt>ARGV0</tt> environment variable.
</p>
<a name="index-nocorrect"></a>
</dd>
<dt><tt>nocorrect</tt></dt>
<dd><p>Spelling correction is not done on any of the words. This must appear
before any other precommand modifier, as it is interpreted immediately,
before any parsing is done. It has no effect in non-interactive shells.
</p>
<a name="index-noglob"></a>
</dd>
<dt><tt>noglob</tt></dt>
<dd><p>Filename generation (globbing) is not performed on any of
the words.
</p>
</dd>
</dl>
<hr>
<a name="Complex-Commands"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Precommand-Modifiers" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Alternate-Forms-For-Complex-Commands" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Complex-Commands-1"></a>
<h2 class="section">6.3 Complex Commands</h2>
<p><a name="index-complex-commands"></a>
<a name="index-commands_002c-complex"></a>
A <em>complex command</em> in zsh is one of the following:
</p>
<dl compact="compact">
<dd><a name="index-if"></a>
<a name="index-if-construct"></a>
</dd>
<dt><tt>if</tt> <var>list</var> <tt>then</tt> <var>list</var> [ <tt>elif</tt> <var>list</var> <tt>then</tt> <var>list</var> ] ... [ <tt>else</tt> <var>list</var> ] <tt>fi</tt></dt>
<dd><p>The <tt>if</tt> <var>list</var> is executed, and if it returns a zero exit status,
the <tt>then</tt> <var>list</var> is executed.
Otherwise, the <tt>elif</tt> <var>list</var> is executed and if its status is zero,
the <tt>then</tt> <var>list</var> is executed.
If each <tt>elif</tt> <var>list</var> returns nonzero status, the <tt>else</tt> <var>list</var>
is executed.
</p>
<a name="index-for"></a>
<a name="index-for-loops"></a>
<a name="index-loops_002c-for"></a>
</dd>
<dt><tt>for</tt> <var>name</var> ... [ <tt>in</tt> <var>word</var> ... ] <var>term</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p>where <var>term</var> is at least one newline or <tt>;</tt>.
Expand the list of <var>word</var>s, and set the parameter
<var>name</var> to each of them in turn, executing
<var>list</var> each time. If the <tt>in</tt> <var>word</var> is omitted,
use the positional parameters instead of the <var>word</var>s.
</p>
<p>More than one parameter <var>name</var> can appear before the list of
<var>word</var>s. If <var>N</var> <var>name</var>s are given, then on each execution of the
loop the next <var>N</var> <var>word</var>s are assigned to the corresponding
parameters. If there are more <var>name</var>s than remaining <var>word</var>s, the
remaining parameters are each set to the empty string. Execution of the
loop ends when there is no remaining <var>word</var> to assign to the first
<var>name</var>. It is only possible for <tt>in</tt> to appear as the first <var>name</var>
in the list, else it will be treated as marking the end of the list.
</p>
</dd>
<dt><tt>for ((</tt> [<var>expr1</var>] <tt>;</tt> [<var>expr2</var>] <tt>;</tt> [<var>expr3</var>] <tt>)) do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p>The arithmetic expression <var>expr1</var> is evaluated first (see
<a href="Arithmetic-Evaluation.html#Arithmetic-Evaluation">Arithmetic Evaluation</a>). The arithmetic expression
<var>expr2</var> is repeatedly evaluated until it evaluates to zero and
when non-zero, <var>list</var> is executed and the arithmetic expression
<var>expr3</var> evaluated. If any expression is omitted, then it behaves
as if it evaluated to 1.
</p>
<a name="index-while"></a>
<a name="index-while-loops"></a>
<a name="index-loops_002c-while"></a>
</dd>
<dt><tt>while</tt> <var>list</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p>Execute the <tt>do</tt> <var>list</var> as long as the <tt>while</tt> <var>list</var>
returns a zero exit status.
</p>
<a name="index-until"></a>
<a name="index-until-loops"></a>
<a name="index-loops_002c-until"></a>
</dd>
<dt><tt>until</tt> <var>list</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p>Execute the <tt>do</tt> <var>list</var> as long as <tt>until</tt> <var>list</var>
returns a nonzero exit status.
</p>
<a name="index-repeat"></a>
<a name="index-repeat-loops"></a>
<a name="index-loops_002c-repeat"></a>
</dd>
<dt><tt>repeat</tt> <var>word</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p><var>word</var> is expanded and treated as an arithmetic expression,
which must evaluate to a number <var>n</var>.
<var>list</var> is then executed <var>n</var> times.
</p>
<p>The <tt>repeat</tt> syntax is disabled by default when the
shell starts in a mode emulating another shell. It can be enabled
with the command ‘<tt>enable -r repeat</tt>’
</p>
<a name="index-case"></a>
<a name="index-case-selection"></a>
<a name="index-selection_002c-case"></a>
</dd>
<dt><tt>case</tt> <var>word</var> <tt>in</tt> [ [<tt>(</tt>] <var>pattern</var> [ <tt>|</tt> <var>pattern</var> ] ... <tt>)</tt> <var>list</var> (<tt>;;</tt>|<tt>;&</tt>|<tt>;|</tt>) ] ... <tt>esac</tt></dt>
<dd><p>Execute the <var>list</var> associated with the first <var>pattern</var>
that matches <var>word</var>, if any. The form of the patterns
is the same as that used for filename generation. See
<a href="Expansion.html#Filename-Generation">Filename Generation</a>.
</p>
<p>Note further that, unless the <tt>SH_GLOB</tt> option is set, the whole
pattern with alternatives is treated by the shell as equivalent to a
group of patterns within parentheses, although white space may appear
about the parentheses and the vertical bar and will be stripped from the
pattern at those points. White space may appear elsewhere in the
pattern; this is not stripped. If the <tt>SH_GLOB</tt> option is set, so
that an opening parenthesis can be unambiguously treated as part of the
case syntax, the expression is parsed into separate words and these are
treated as strict alternatives (as in other shells).
</p>
<p>If the <var>list</var> that is executed is terminated with <tt>;&</tt> rather than
<tt>;;</tt>, the following list is also executed. The rule for
the terminator of the following list <tt>;;</tt>, <tt>;&</tt> or <tt>;|</tt> is
applied unless the <tt>esac</tt> is reached.
</p>
<p>If the <var>list</var> that is executed is terminated with <tt>;|</tt> the
shell continues to scan the <var>pattern</var>s looking for the next match,
executing the corresponding <var>list</var>, and applying the rule for
the corresponding terminator <tt>;;</tt>, <tt>;&</tt> or <tt>;|</tt>.
Note that <var>word</var> is not re-expanded; all applicable <var>pattern</var>s
are tested with the same <var>word</var>.
</p>
<a name="index-select"></a>
<a name="index-user-selection"></a>
<a name="index-selection_002c-user"></a>
</dd>
<dt><tt>select</tt> <var>name</var> [ <tt>in</tt> <var>word</var> ... <var>term</var> ] <tt>do</tt> <var>list</var> <tt>done</tt></dt>
<dd><p>where <var>term</var> is one or more newline or <tt>;</tt> to terminate the <var>word</var>s.
<a name="index-REPLY_002c-use-of"></a>
Print the set of <var>word</var>s, each preceded by a number.
If the <tt>in</tt> <var>word</var> is omitted, use the positional parameters.
The <tt>PROMPT3</tt> prompt is printed and a line is read from the line editor
if the shell is interactive and that is active, or else standard input.
If this line consists of the
number of one of the listed <var>word</var>s, then the parameter <var>name</var>
is set to the <var>word</var> corresponding to this number.
If this line is empty, the selection list is printed again.
Otherwise, the value of the parameter <var>name</var> is set to null.
The contents of the line read from standard input is saved
in the parameter <tt>REPLY</tt>. <var>list</var> is executed
for each selection until a break or end-of-file is encountered.
</p>
<a name="index-subshell"></a>
</dd>
<dt><tt>(</tt> <var>list</var> <tt>)</tt></dt>
<dd><p>Execute <var>list</var> in a subshell. Traps set by the <tt>trap</tt> builtin
are reset to their default values while executing <var>list</var>.
</p>
</dd>
<dt><tt>{</tt> <var>list</var> <tt>}</tt></dt>
<dd><p>Execute <var>list</var>.
</p>
<a name="index-always"></a>
<a name="index-always-blocks"></a>
<a name="index-try-blocks"></a>
</dd>
<dt><tt>{</tt> <var>try-list</var> <tt>} always {</tt> <var>always-list</var> <tt>}</tt></dt>
<dd><p>First execute <var>try-list</var>. Regardless of errors, or <tt>break</tt>,
<tt>continue</tt>, or <tt>return</tt> commands encountered within <var>try-list</var>,
execute <var>always-list</var>. Execution then continues from the
result of the execution of <var>try-list</var>; in other words, any error,
or <tt>break</tt>, <tt>continue</tt>, or <tt>return</tt> command is treated in the
normal way, as if <var>always-list</var> were not present. The two
chunks of code are referred to as the ‘try block’ and the ‘always block’.
</p>
<p>Optional newlines or semicolons may appear after the <tt>always</tt>;
note, however, that they may <em>not</em> appear between the preceding
closing brace and the <tt>always</tt>.
</p>
<p>An ‘error’ in this context is a condition such as a syntax error which
causes the shell to abort execution of the current function, script, or
list. Syntax errors encountered while the shell is parsing the
code do not cause the <var>always-list</var> to be executed. For example,
an erroneously constructed <tt>if</tt> block in <tt>try-list</tt> would cause the
shell to abort during parsing, so that <tt>always-list</tt> would not be
executed, while an erroneous substitution such as <tt>${*foo*}</tt> would
cause a run-time error, after which <tt>always-list</tt> would be executed.
</p>
<p>An error condition can be tested and reset with the special integer
variable <tt>TRY_BLOCK_ERROR</tt>. Outside an <tt>always-list</tt> the value is
irrelevant, but it is initialised to <tt>-1</tt>. Inside <tt>always-list</tt>, the
value is 1 if an error occurred in the <tt>try-list</tt>, else 0. If
<tt>TRY_BLOCK_ERROR</tt> is set to 0 during the <tt>always-list</tt>, the error
condition caused by the <tt>try-list</tt> is reset, and shell execution
continues normally after the end of <tt>always-list</tt>. Altering the value
during the <tt>try-list</tt> is not useful (unless this forms part of an
enclosing <tt>always</tt> block).
</p>
<p>Regardless of <tt>TRY_BLOCK_ERROR</tt>, after the end of <tt>always-list</tt> the
normal shell status <tt>$?</tt> is the value returned from <tt>try-list</tt>.
This will be non-zero if there was an error, even if <tt>TRY_BLOCK_ERROR</tt>
was set to zero.
</p>
<p>The following executes the given code, ignoring any errors it causes.
This is an alternative to the usual convention of protecting code by
executing it in a subshell.
</p>
<div class="example">
<pre class="example">{
# code which may cause an error
} always {
# This code is executed regardless of the error.
(( TRY_BLOCK_ERROR = 0 ))
}
# The error condition has been reset.
</pre></div>
<p>An <tt>exit</tt> command (or a <tt>return</tt> command executed at the outermost
function level of a script) encountered in <tt>try-list</tt> does <em>not</em> cause
the execution of <var>always-list</var>. Instead, the shell exits immediately
after any <tt>EXIT</tt> trap has been executed.
</p>
<a name="index-function"></a>
</dd>
<dt><tt>function</tt> <var>word</var> ... [ <tt>()</tt> ] [ <var>term</var> ] <tt>{</tt> <var>list</var> <tt>}</tt></dt>
<dt><var>word</var> ... <tt>()</tt> [ <var>term</var> ] <tt>{</tt> <var>list</var> <tt>}</tt></dt>
<dt><var>word</var> ... <tt>()</tt> [ <var>term</var> ] <var>command</var></dt>
<dd><p>where <var>term</var> is one or more newline or <tt>;</tt>.
Define a function which is referenced by any one of <var>word</var>.
Normally, only one <var>word</var> is provided; multiple <var>word</var>s
are usually only useful for setting traps.
The body of the function is the <var>list</var> between
the <tt>{</tt> and <tt>}</tt>. See <a href="Functions.html#Functions">Functions</a>.
</p>
<p>If the option <tt>SH_GLOB</tt> is set for compatibility with other shells, then
whitespace may appear between the left and right parentheses when
there is a single <var>word</var>; otherwise, the parentheses will be treated as
forming a globbing pattern in that case.
</p>
<p>In any of the forms above, a redirection may appear outside the
function body, for example
</p>
<div class="example">
<pre class="example">func() { ... } 2>&1
</pre></div>
<p>The redirection is stored with the function and applied whenever the
function is executed. Any variables in the redirection are expanded
at the point the function is executed, but outside the function scope.
</p>
<a name="index-timing"></a>
<a name="index-time"></a>
</dd>
<dt><tt>time</tt> [ <var>pipeline</var> ]</dt>
<dd><p>The <var>pipeline</var> is executed, and timing statistics are
reported on the standard error in the form specified
by the <tt>TIMEFMT</tt> parameter.
If <var>pipeline</var> is omitted, print statistics about the
shell process and its children.
</p>
<a name="index-conditional-expression"></a>
<a name="index-_005b_005b"></a>
</dd>
<dt><tt>[[</tt> <var>exp</var> <tt>]]</tt></dt>
<dd><p>Evaluates the conditional expression <var>exp</var>
and return a zero exit status if it is true.
See <a href="Conditional-Expressions.html#Conditional-Expressions">Conditional Expressions</a>
for a description of <var>exp</var>.
</p>
</dd>
</dl>
<hr>
<a name="Alternate-Forms-For-Complex-Commands"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Complex-Commands" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Reserved-Words" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Alternate-Forms-For-Complex-Commands-1"></a>
<h2 class="section">6.4 Alternate Forms For Complex Commands</h2>
<p><a name="index-alternate-forms-for-complex-commands"></a>
<a name="index-commands_002c-alternate-forms-for-complex"></a>
Many of zsh’s complex commands have alternate forms. These are
non-standard and are likely not to be obvious even to seasoned shell
programmers; they should not be used anywhere that portability of shell
code is a concern.
</p>
<p>The short versions below only work if <var>sublist</var> is of the form ‘<tt>{</tt>
<var>list</var> <tt>}</tt>’ or if the <tt>SHORT_LOOPS</tt> option is set. For the <tt>if</tt>,
<tt>while</tt> and <tt>until</tt> commands, in both these cases the test part of the
loop must also be suitably delimited, such as by ‘<tt>[[</tt> <var>...</var> <tt>]]</tt>’ or
‘<tt>((</tt> <var>...</var> <tt>))</tt>’,
else the end of the test will not be recognized. For the
<tt>for</tt>, <tt>repeat</tt>, <tt>case</tt> and <tt>select</tt> commands no such special form
for the arguments is necessary, but the other condition (the special form
of <var>sublist</var> or use of the <tt>SHORT_LOOPS</tt> option) still applies.
</p>
<dl compact="compact">
<dt><tt>if</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt> [ <tt>elif</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt> ] ... [ <tt>else {</tt> <var>list</var> <tt>}</tt> ]</dt>
<dd><p>An alternate form of <tt>if</tt>. The rules mean that
</p>
<div class="example">
<pre class="example">if [[ -o ignorebraces ]] {
print yes
}
</pre></div>
<p>works, but
</p>
<div class="example">
<pre class="example">if true { # Does not work!
print yes
}
</pre></div>
<p>does <em>not</em>, since the test is not suitably delimited.
</p>
</dd>
<dt><tt>if</tt> <var>list</var> <var>sublist</var></dt>
<dd><p>A short form of the alternate <tt>if</tt>. The same limitations on the form of
<var>list</var> apply as for the previous form.
</p>
</dd>
<dt><tt>for</tt> <var>name</var> ... <tt>(</tt> <var>word</var> ... <tt>)</tt> <var>sublist</var></dt>
<dd><p>A short form of <tt>for</tt>.
</p>
</dd>
<dt><tt>for</tt> <var>name</var> ... [ <tt>in</tt> <var>word</var> ... ] <var>term</var> <var>sublist</var></dt>
<dd><p>where <var>term</var> is at least one newline or <tt>;</tt>.
Another short form of <tt>for</tt>.
</p>
</dd>
<dt><tt>for ((</tt> [<var>expr1</var>] <tt>;</tt> [<var>expr2</var>] <tt>;</tt> [<var>expr3</var>] <tt>))</tt> <var>sublist</var></dt>
<dd><p>A short form of the arithmetic <tt>for</tt> command.
</p>
<a name="index-foreach"></a>
</dd>
<dt><tt>foreach</tt> <var>name</var> ... <tt>(</tt> <var>word</var> ... <tt>)</tt> <var>list</var> <tt>end</tt></dt>
<dd><p>Another form of <tt>for</tt>.
</p>
</dd>
<dt><tt>while</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt></dt>
<dd><p>An alternative form of <tt>while</tt>. Note the limitations on the form of
<var>list</var> mentioned above.
</p>
</dd>
<dt><tt>until</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt></dt>
<dd><p>An alternative form of <tt>until</tt>. Note the limitations on the form of
<var>list</var> mentioned above.
</p>
</dd>
<dt><tt>repeat</tt> <var>word</var> <var>sublist</var></dt>
<dd><p>This is a short form of <tt>repeat</tt>.
</p>
</dd>
<dt><tt>case</tt> <var>word</var> <tt>{</tt> [ [<tt>(</tt>] <var>pattern</var> [ <tt>|</tt> <var>pattern</var> ] ... <tt>)</tt> <var>list</var> (<tt>;;</tt>|<tt>;&</tt>|<tt>;|</tt>) ] ... <tt>}</tt></dt>
<dd><p>An alternative form of <tt>case</tt>.
</p>
</dd>
<dt><tt>select</tt> <var>name</var> [ <tt>in</tt> <var>word</var> ... <var>term</var> ] <var>sublist</var></dt>
<dd><p>where <var>term</var> is at least one newline or <tt>;</tt>.
A short form of <tt>select</tt>.
</p>
</dd>
<dt><tt>function</tt> <var>word</var> ... [ <tt>()</tt> ] [ <var>term</var> ] <var>sublist</var></dt>
<dd><p>This is a short form of <tt>function</tt>.
</p>
</dd>
</dl>
<hr>
<a name="Reserved-Words"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Alternate-Forms-For-Complex-Commands" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Errors" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Reserved-Words-1"></a>
<h2 class="section">6.5 Reserved Words</h2>
<p><a name="index-reserved-words"></a>
<a name="index-disable_002c-use-of"></a>
The following words are recognized as reserved words when used as the first
word of a command unless quoted or disabled using <tt>disable -r</tt>:
</p>
<p><tt>do done esac then elif else fi for case
if while function repeat time until
select coproc nocorrect foreach end ! [[ { }
declare export float integer local readonly typeset</tt>
</p>
<p>Additionally, ‘<tt>}</tt>’ is recognized in any position if neither the
<tt>IGNORE_BRACES</tt> option nor the <tt>IGNORE_CLOSE_BRACES</tt> option is set.
</p><hr>
<a name="Errors"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Reserved-Words" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Comments" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Errors-1"></a>
<h2 class="section">6.6 Errors</h2>
<p><a name="index-errors_002c-handling-of"></a>
Certain errors are treated as fatal by the shell: in an interactive
shell, they cause control to return to the command line, and in a
non-interactive shell they cause the shell to be aborted. In older
versions of zsh, a non-interactive shell running a script would not
abort completely, but would resume execution at the next command to be
read from the script, skipping the remainder of any functions or
shell constructs such as loops or conditions; this somewhat illogical
behaviour can be recovered by setting the option <tt>CONTINUE_ON_ERROR</tt>.
</p>
<p>Fatal errors found in non-interactive shells include:
</p>
<ul>
<li> Failure to parse shell options passed when invoking the shell
</li><li> Failure to change options with the <tt>set</tt> builtin
</li><li> Parse errors of all sorts, including failures to parse
mathematical expressions
</li><li> Failures to set or modify variable behaviour with <tt>typeset</tt>,
<tt>local</tt>, <tt>declare</tt>, <tt>export</tt>, <tt>integer</tt>, <tt>float</tt>
</li><li> Execution of incorrectly positioned loop control structures
(<tt>continue</tt>, <tt>break</tt>)
</li><li> Attempts to use regular expression with no regular expression
module available
</li><li> Disallowed operations when the <tt>RESTRICTED</tt> options is set
</li><li> Failure to create a pipe needed for a pipeline
</li><li> Failure to create a multio
</li><li> Failure to autoload a module needed for a declared shell feature
</li><li> Errors creating command or process substitutions
</li><li> Syntax errors in glob qualifiers
</li><li> File generation errors where not caught by the option <tt>BAD_PATTERN</tt>
</li><li> All bad patterns used for matching within case statements
</li><li> File generation failures where not caused by <tt>NO_MATCH</tt> or
similar options
</li><li> All file generation errors where the pattern was used to create a
multio
</li><li> Memory errors where detected by the shell
</li><li> Invalid subscripts to shell variables
</li><li> Attempts to assign read-only variables
</li><li> Logical errors with variables such as assignment to the wrong type
</li><li> Use of invalid variable names
</li><li> Errors in variable substitution syntax
</li><li> Failure to convert characters in <tt>$'</tt>...<tt>'</tt> expressions
</li></ul>
<p>If the <tt>POSIX_BUILTINS</tt> option is set, more errors associated with
shell builtin commands are treated as fatal, as specified by the POSIX
standard.
</p>
<hr>
<a name="Comments"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Errors" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Aliasing" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Comments-1"></a>
<h2 class="section">6.7 Comments</h2>
<p><a name="index-comments"></a>
<a name="index-INTERACTIVE_005fCOMMENTS_002c-use-of"></a>
<a name="index-histchars_002c-use-of"></a>
In non-interactive shells, or in interactive shells with the
<tt>INTERACTIVE_COMMENTS</tt> option set, a word beginning
with the third character of the <tt>histchars</tt> parameter
(‘<tt>#</tt>’ by default) causes that word and all the following
characters up to a newline to be ignored.
</p><hr>
<a name="Aliasing"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Comments" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="#Quoting" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Aliasing-1"></a>
<h2 class="section">6.8 Aliasing</h2>
<p><a name="index-aliasing"></a>
Every eligible <em>word</em> in the shell input is checked to see if there
is an alias defined for it.
If so, it is replaced by the text of the alias if it is in command
position (if it could be the first word of a simple command),
or if the alias is global.
If the replacement text ends with a space, the next word in the shell input
is always eligible for purposes of alias expansion.
<a name="index-alias_002c-use-of"></a>
<a name="index-aliases_002c-global"></a>
An alias is defined using the <tt>alias</tt> builtin; global aliases
may be defined using the <tt>-g</tt> option to that builtin.
</p>
<p>A <em>word</em> is defined as:
</p>
<ul>
<li> Any plain string or glob pattern
</li><li> Any quoted string, using any quoting method (note that the quotes
must be part of the alias definition for this to be eligible)
</li><li> Any parameter reference or command substitution
</li><li> Any series of the foregoing, concatenated without whitespace or
other tokens between them
</li><li> Any reserved word (<tt>case</tt>, <tt>do</tt>, <tt>else</tt>, etc.)
</li><li> With global aliasing, any command separator, any redirection
operator, and ‘<tt>(</tt>’ or ‘<tt>)</tt>’ when not part of a glob pattern
</li></ul>
<p>It is not presently possible to alias the ‘<tt>((</tt>’ token that
introduces arithmetic expressions, because until a full statement has been
parsed, it cannot be distinguished from two consecutive ‘<tt>(</tt>’
tokens introducing nested subshells.
</p>
<p>When <tt>POSIX_ALIASES</tt> is set, only plain unquoted strings are eligible
for aliasing. The <tt>alias</tt> builtin does not reject ineligible aliases,
but they are not expanded.
</p>
<p>Alias expansion is done on the shell input before any other expansion
except history expansion. Therefore, if an alias is defined for the
word <tt>foo</tt>, alias expansion may be avoided by quoting part of the
word, e.g. <tt>\foo</tt>. Any form of quoting works, although there is
nothing to prevent an alias being defined for the quoted form such as
<tt>\foo</tt> as well. Also, if a separator such as <tt>&&</tt> is aliased,
<tt>\&&</tt> turns into the two tokens <tt>\&</tt> and <tt>&</tt>, each of which may
have been aliased separately. Similarly for <tt>\<<</tt>, <tt>\>|</tt>, etc.
</p>
<p>For use with completion, which would remove an initial backslash followed
by a character that isn’t special, it may be more convenient to quote the
word by starting with a single quote, i.e. <tt>'foo</tt>; completion will
automatically add the trailing single quote.
</p>
<p>There is a commonly encountered problem with aliases
illustrated by the following code:
</p>
<div class="example">
<pre class="example">alias echobar='echo bar'; echobar
</pre></div>
<p>This prints a message that the command <tt>echobar</tt> could not be found.
This happens because aliases are expanded when the code is read in;
the entire line is read in one go, so that when <tt>echobar</tt> is executed it
is too late to expand the newly defined alias. This is often
a problem in shell scripts, functions, and code executed with ‘<tt>source</tt>’
or ‘<tt>.</tt>’. Consequently, use of functions rather than aliases is
recommended in non-interactive code.
</p>
<p>Note also the unhelpful interaction of aliases and function definitions:
</p>
<div class="example">
<pre class="example">alias func='noglob func'
func() {
echo Do something with $*
}
</pre></div>
<p>Because aliases are expanded in function definitions, this causes the
following command to be executed:
</p>
<div class="example">
<pre class="example">noglob func() {
echo Do something with $*
}
</pre></div>
<p>which defines <tt>noglob</tt> as well as <tt>func</tt> as functions with the
body given. To avoid this, either quote the name <tt>func</tt> or use the
alternative function definition form ‘<tt>function func</tt>’. Ensuring the
alias is defined after the function works but is problematic if the
code fragment might be re-executed.
</p>
<hr>
<a name="Quoting"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Aliasing" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Quoting-1"></a>
<h2 class="section">6.9 Quoting</h2>
<p><a name="index-quoting"></a>
A character may be <em>quoted</em> (that is, made
to stand for itself) by preceding it with a ‘<tt>\</tt>’.
‘<tt>\</tt>’ followed by a newline is ignored.
</p>
<p>A string enclosed between ‘<tt>$'</tt>’ and ‘<tt>'</tt>’ is
processed the same way as the string arguments of the
<tt>print</tt> builtin, and the resulting string is considered to be
entirely quoted. A literal ‘<tt>'</tt>’ character can be included in the
string by using the ‘<tt>\'</tt>’ escape.
</p>
<p><a name="index-RC_005fQUOTES_002c-use-of"></a>
All characters enclosed between a pair of single quotes (<tt>''</tt>) that
is not preceded by a ‘<tt>$</tt>’ are quoted. A single quote cannot appear
within single quotes unless the option <tt>RC_QUOTES</tt> is set, in which case
a pair of single quotes are turned into a single quote. For example,
</p>
<div class="example">
<pre class="example">print ''''
</pre></div>
<p>outputs nothing apart from a newline if <tt>RC_QUOTES</tt> is not set, but one
single quote if it is set.
</p>
<p>Inside double quotes (<tt>""</tt>), parameter and
command substitution occur, and ‘<tt>\</tt>’ quotes the characters
‘<tt>\</tt>’, ‘<tt>`</tt>’, ‘<tt>"</tt>’, and ‘<tt>$</tt>’.
</p><hr>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p><font size="-1">
This document was generated on <em>April 6, 2018</em> using <a href="http://www.gnu.org/software/texinfo/"><em>texi2any</em></a>.
</font></p>
<font size="-1">Zsh version 5.4.2, released on August 27, 2017.</font>
</body>
</html>
|