Pegasus InfoCorp: Web site design and web software development company

ZSHMISC (1)

Everything and then some

SYNOPSIS

    Everything I haven't put somewhere else

SHELL GRAMMAR

    A simple command is a sequence of optional parameter assignments followed by blank-separated words, with optional redirections interspersed. 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.

    A pipeline is a sequence of one or more commands separated by | or |&. |& is shorthand for 2>&1 |. The standard output of each command is connected to the standard input of the next command in the pipeline. If a pipeline is preceded by coproc, 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 >&p and <&p redirection operators. The value of a pipeline is the value of the last command. If a pipeline is preceded by a !, the value of that pipeline is the logical NOT of the value of the last command.

    A sublist is a sequence of one or more pipelines separated by && or |\||. If two pipelines are separated by &&, the second pipeline is executed only if the first is successful (returns a zero value). If two pipelines are separated by |\||, the second is executed only if the first is unsuccessful (returns a nonzero value). Both operators have equal precedence and are left associative.

    A list is a sequence of zero or more sublists separated by, and optionally terminated by, ;, &, &|, &! or a newline. Normally the shell waits for each list to finish before executing the next one. If a list is terminated by &, &| or &!, the shell executes it in the background, and does not wait for it to finish.

PRECOMMAND MODIFIERS

    A simple command may be preceded by a precommand modifier which will alter how the command is interpreted. These modifiers are shell builtin commands with the exception of nocorrect which is a reserved word.

    -

      The command is executed with a - prepended to its argv[0] string.

    noglob

      Filename generation (globbing) is not performed on any of the words.

    nocorrect

      Spelling correction is not done on any of the words.

    exec

      The command is executed in the parent shell without forking.

    command

      The command word is taken to be the name of an external command, rather than a shell function or builtin.

COMPLEX COMMANDS

    A complex command in zsh is one of the following:

    if list then list [ elif list then list ] ... [ else list ] fi

      The if list is executed, and, if it returns a zero exit status, the then list is executed. Otherwise, the elif list is executed and, if its value is zero, the then list is executed. If each elif list returns nonzero, the else list is executed.

    for name [ in word ... term ] do list done

      where term is one ore more newline or ;. Expand the list of words, and set the parameter name to each of them in turn, executing list each time. If the in word is omitted, use the positional parameters instead of the words.

    while list do list done

      Execute the do list as long as the while list returns a zero exit status.

    until list do list done

      Execute the do list as long as until list returns a nonzero exit status.

    repeat word do list done

      word is expanded and treated as an arithmetic expression, which must evaluate to a number n. list is then executed n times.

    case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

      Execute the list associated with the first pattern that matches word, if any. The form of the patterns is the same as that used for filename generation. See Filename Generation below.

    select name [ in word ... term ] do list done

      where term is one ore more newline or ;. Print the set of words, each preceded by a number. If the in word is omitted, use the positional parameters. The PROMPT3 prompt is printed and a line is read from standard input. If this line consists of the number of one of the listed words, then the parameter name is set to the word corresponding to this number. If this line is empty, the selection list is printed again. Otherwise, the value of the parameter name is set to null. The contents of the line read from standard input is saved in the parameter REPLY. list is executed for each selection until a break or end-of-file is encountered.

    ( list )

      Execute list in a subshell. Traps set by the trap builtin are reset to their default values while executing list.

    { list }

      Execute list.

    function word ... [ (\|) ] [ term ] { list }

    word ... (\|) [ term ] { list }

    word ... (\|) [ term ] command

      where term is one ore more newline or ;. Define a function which is referenced by any one of word. Normally, only one word is provided; multiple words are usually only useful for setting traps. The body of the function is the list between the { and }. See FUNCTIONS below.

    time [ pipeline ]

      The pipeline is executed, and timing statistics are reported on the standard error in the form specified by the TIMEFMT parameter. If pipeline is omitted, print statistics about the shell process and its children.

    [[ exp ]]

      Evaluates the conditional expression exp and return a zero exit status if it is true. See Conditional Expressions below for a description of exp.

ALTERNATE FORMS FOR COMPLEX COMMANDS

    Many of zsh's complex commands have alternate forms. These particular versions of complex commands should be considered deprecated and may be removed in the future. The versions in the previous section should be preferred instead. The short versions below only work if sublist is of the form { list } or if the NO_SHORT_LOOPS option is not set.

    if list { list } [ elif list { list } ] ... [ else { list } ]

      An alternate form of if.

    if list sublist

      A short form of previous one.

    for name ( word ... ) sublist

      A short form of for.

    for name [ in word ... term ] sublist

      where term is one ore more newline or ;. Another short form of for.

    foreach name ( word ... ) list end

      Another form of for.

    while list { list }

      An alternative form of while.

    until list { list }

      An alternative form of until.

    repeat word sublist

      This is a short form of repeat.

    case word { [ [(] pattern [ | pattern ] ... ) list ;; ] ... }

      An alternative form of case.

    select name [ in word term ] sublist

      where term is one ore more newline or ;. A short form of select.

RESERVED WORDS

    The following words with the exception of } are recognized as reserved words when used as the first word of a command unless quoted or disabled using disable -r:

      do done esac then elif else fi for case if while function repeat time until select coproc nocorrect foreach end ! [[ { }

COMMENTS

    In noninteractive shells, or in interactive shells with the INTERACTIVE_COMMENTS option set, a word beginning with the third character of the histchars parameter (`#' by default) causes that word and all the following characters up to a newline to be ignored.

ALIASING

    Every token 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 text ends with a space, the next word in the shell input is treated as though it were in command position for purposes of alias expansion. An alias is defined using the alias builtin; global aliases may be defined using the -g option to that builtin.

    Alias substitution is done on the shell input before any other substitution except history substitution. Therefore, if an alias is defined for the word foo, alias substitution may be avoided by quoting part of the word, e.g. \efoo. But there is nothing to prevent an alias being defined for \efoo as well.

QUOTING

    A character may be quoted (that is, made to stand for itself) by preceding it with a \e\|. \e followed by a newline is ignored. All characters enclosed between a pair of single quotes ('') are quoted. A single quote cannot appear within single quotes. Inside double quotes (""), parameter and command substitution occurs, and \e quotes the characters \e\|, `, ", and $.

REDIRECTION

    Before a command is executed, its input and output may be redirected. The following may appear anywhere in a simple command or may precede or follow a complex command. Substitution occurs before word or digit is used except as noted below. If the result of substitution on word produces more than one filename, redirection occurs for each separate filename in turn.

    < word

      Open file word as standard input.

    <> word

      Open file word for reading and writing as standard input. If the file does not exist then it is created.

    > word

      Open file word as standard output. If the file does not exist then it is created. If the file exists, and the CLOBBER option is unset, this causes an error; otherwise, it is truncated to zero length.

    >| word

    >! word

      Same as > , except that the file is truncated to zero length if it exists, even if CLOBBER is unset.

    >> word

      Open file word as standard output. If the file exists then output is appended to it. If the file does not exist, and the CLOBBER option is unset, this causes an error; otherwise, the file is created.

    >>| word

    >>! word

      Same as >> , except that the file is created if it does not exist, even if CLOBBER is unset.

    <<[-] word

      The shell input is read up to a line that is the same as word , or to an end-of-file. No parameter substitution, command substitution or filename generation is performed on word . The resulting document, called a here-document , becomes the standard input. If any character of word is quoted with single or double quotes or a \e, no interpretation is placed upon the characters of the document. Otherwise, parameter and command substitution occurs, \e followed by a newline is removed, and \e must be used to quote the characters \e, $, `, and the first character of word. If <<- is used, then all leading tabs are stripped from word and from the document.

    <<< word

      Perform shell expansion on word and pass the result to standard input.

    <& digit

      The standard input is duplicated from file descriptor digit (see dup (2)). Similarly for standard output using >&digit.

    >& word

      Same as > word 2>&1.

    >>& word

      Same as >> word 2>&1.

    <&-

      Close the standard input.

    >&-

      Close the standard output.

    <&p

      The input from the coprocess is moved to the standard input.

    >&p

      The output to the coprocess is moved to the standard output.

    If one of the above is preceded by a digit, then the file descriptor referred to is that specified by the digit (instead of the default 0 or 1). The order in which redirections are specified is significant. The shell evaluates each redirection in terms of the ( file descriptor , file ) association at the time of evaluation. For example:

      .\|.\|. \|1>fname\^ 2>&1

    first associates file descriptor 1 with file fname . It then associates file descriptor 2 with the file associated with file descriptor 1 (that is, fname ). If the order of redirections were reversed, file descriptor 2 would be associated with the terminal (assuming file descriptor 1 had been) and then file descriptor 1 would be associated with file fname .

    If the user tries to open a file descriptor for writing more than once, the shell opens the file descriptor as a pipe to a process that copies its input to all the specified outputs, similar to tee(1), provided the MULTIOS option is set. Thus:

      date >foo >bar

    writes the date to two files, named "foo" and "bar". Note that a pipe is an implicit indirection; thus

      date >foo | cat

    writes the date to the file "foo", and also pipes it to cat.

    If the MULTIOS option is set, the word after a redirection operator is also subjected to filename generation (globbing). Thus

      : > *

    will truncate all files in the current directory, assuming there's at least one. (Without the MULTIOS option, it would create an empty file called "*".)

    If the user tries to open a file descriptor for reading more than once, the shell opens the file descriptor as a pipe to a process that copies all the specified inputs to its output in the order specified, similar to cat(1), provided the MULTIOS option is set. Thus

      sort <foo <fubar

    or even

      sort <f{oo,ubar}

    is equivalent to "cat foo fubar | sort". Similarly, you can do

      echo exit 0 >> *.sh

    Note that a pipe is in implicit indirection; thus

      cat bar | sort <foo

    is equivalent to "cat bar foo | sort" (note the order of the inputs).

    If the MULTIOS option is un set, each redirection replaces the previous redirection for that file descriptor. However, all files redirected to are actually opened, so

      echo foo > bar > baz

    when MULTIOS is unset will truncate bar, and write "foo" into baz.

    If a simple command consists of one or more redirection operators and zero or more parameter assignments, but no command name, the command cat is assumed. Thus

      < file

    prints the contents of file.

    If a command is followed by & and job control is not active, then the default standard input for the command is the empty file /dev/null . Otherwise, the environment for the execution of a command contains the file descriptors of the invoking shell as modified by input/output specifications.

COMMAND EXECUTION

    If a command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, the function is invoked as described below in FUNCTIONS. If there exists a shell builtin by that name, the builtin is invoked.

    Otherwise, the shell searches each element of path for a directory containing an executable file by that name. If the search is unsuccessful, the shell prints an error message and returns a nonzero exit status.

    If execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a shell script. /bin/sh is spawned to execute it. If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell will execute the specified interpreter on operating systems that do not handle this executable format in the kernel.

FUNCTIONS

    The function reserved word is used to define shell functions. Shell functions are read in and stored internally. Alias names are resolved when the function is read. Functions are executed like commands with the arguments passed as positional parameters. (See Execution below).

    Functions execute in the same process as the caller and share all files and present working directory with the caller. A trap on EXIT set inside a function is executed after the function completes in the environment of the caller.

    The return builtin is used to return from function calls.

    Function identifiers can be listed with the functions builtin. Functions can be undefined with the unfunction builtin.

    The following functions, if defined, have special meaning to the shell:

    chpwd

      Executed whenever the current working directory is changed.

    precmd

      Executed before each prompt.

    periodic

      If the parameter PERIOD is set, this function is executed every PERIOD seconds, just before a prompt.

    TRAPxxx

      If defined and non-null, this function will be executed whenever the shell catches a signal SIGxxx, where xxx is a signal name as specified for the kill builtin (see below). The signal number will be passed as the first parameter to the function. In addition, TRAPZERR is executed whenever a command has a non-zero exit status, TRAPDEBUG is executed after each command, and TRAPEXIT is executed when the shell exits, or when the current function exits if defined inside a function. If a function of this form is defined and null, the shell and processes spawned by it will ignore SIGxxx.

JOBS

    If the MONITOR option is set, an interactive shell associates a job with each pipeline. It keeps a table of current jobs, printed by the jobs command, and assigns them small integer numbers. When a job is started asynchronously with & , the shell prints a line which looks like: