Copyright (C) 2000-2012 |
Manpages ZSHMISCSection: User Commands (1)Updated: August 14, 2002 Index Return to Main Contents NAMEzshmisc - everything and then someSIMPLE COMMANDS & PIPELINESA 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. For example,
is a simple command with arguments. A pipeline is either a simple command, or a sequence of two or more simple commands where each command is separated from the next by `|' or `|&'. Where commands are separated by `|', the standard output of the first command is connected to the standard input of the next. `|&' is shorthand for `2>&1 |', 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 `!' in which case the value is the logical inverse of the value of the last command. For example,
is a pipeline, where the output (`foo' plus a newline) of the first command will be passed to the input of the second. 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 or with `print -p' and `read -p'. A pipeline cannot be preceded by both `coproc' and `!'. If job control is active, the coprocess can be treated in other than input and output as an ordinary background job. A sublist is either a single pipeline, or a sequence of two or more pipelines separated by `&&' or `||'. If two pipelines are separated by `&&', the second pipeline is executed only if the first succeeds (returns a zero value). If two pipelines are separated by `||', the second is executed only if the first fails (returns a nonzero value). Both operators have equal precedence and are left associative. The value of the sublist is the value of the last pipeline executed. For example,
is a sublist consisting of two pipelines, the second just a simple command which will be executed if and only if the grep command returns a zero value. If it does not, the value of the sublist is that return value, else it is the value returned by the print (almost certainly zero). A list is a sequence of zero or more sublists, in which each sublist is terminated by `;', `&', `&|', `&!', 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 `(...)' or `{...}'. When a sublist is terminated by `;' or newline, the shell waits for it to finish before executing the next sublist. If a sublist is terminated by a `&', `&|', or `&!', 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. 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. PRECOMMAND MODIFIERSA 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.
COMPLEX COMMANDSA complex command in zsh is one of the following:
ALTERNATE FORMS FOR COMPLEX COMMANDSMany 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 SHORT_LOOPS option is set. For the if, while and until commands, in both these cases the test part of the loop must also be suitably delimited, such as by `[[ ... ]]' or `(( ... ))', else the end of the test will not be recognized. For the for, repeat, case and select commands no such special form for the arguments is necessary, but the other condition (the special form of sublist or use of the SHORT_LOOPS option) still applies.
RESERVED WORDSThe following words 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 ! [[ { } Additionally, `}' is recognized in any position if the IGNORE_BRACES option is not set. COMMENTSIn 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.ALIASINGEvery 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 expansion is done on the shell input before any other expansion except history expansion. Therefore, if an alias is defined for the word foo, alias expansion may be avoided by quoting part of the word, e.g. \foo. But there is nothing to prevent an alias being defined for \foo as well. QUOTINGA character may be quoted (that is, made to stand for itself) by preceding it with a `\'. `\' followed by a newline is ignored.A string enclosed between `$'' and `'' is processed the same way as the string arguments of the print builtin, and the resulting string is considered to be entirely quoted. A literal `'' character can be included in the string by using the `\'' escape. All characters enclosed between a pair of single quotes ('') that is not preceded by a `$' are quoted. A single quote cannot appear within single quotes unless the option RC_QUOTES is set, in which case a pair of single quotes are turned into a single quote. For example,
outputs nothing apart from a newline if RC_QUOTES is not set, but one single quote if it is set. Inside double quotes (""), parameter and command substitution occur, and `\' quotes the characters `\', ``', `"', and `$'. REDIRECTIONIf 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.The following may appear anywhere in a simple command or may precede or follow a complex command. Expansion 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.
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:
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. MULTIOSIf 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, provided the MULTIOS option is set, as it is by default. Thus:
writes the date to two files, named `foo' and `bar'. Note that a pipe is an implicit redirection; thus
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 `*'.) Similarly, you can do
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, provided the MULTIOS option is set. Thus
or even
is equivalent to `cat foo fubar | sort'. Note that a pipe is an implicit redirection; thus
is equivalent to `cat bar foo | sort' (note the order of the inputs). If the MULTIOS option is unset, each redirection replaces the previous redirection for that file descriptor. However, all files redirected to are actually opened, so
when MULTIOS is unset will truncate bar, and write `foo' into baz. REDIRECTIONS WITH NO COMMANDWhen a simple command consists of one or more redirection operators and zero or more parameter assignments, but no command name, zsh can behave in several ways.If the parameter NULLCMD is not set or the option CSH_NULLCMD is set, an error is caused. This is the csh behavior and CSH_NULLCMD is set by default when emulating csh. If the option SH_NULLCMD is set, the builtin `:' is inserted as a command with the given redirections. This is the default when emulating sh or ksh. Otherwise, if the parameter NULLCMD is set, its value will be used as a command with the given redirections. If both NULLCMD and READNULLCMD are set, then the value of the latter will be used instead of that of the former when the redirection is an input. The default for NULLCMD is `cat' and for READNULLCMD is `more'. Thus
shows the contents of file on standard output, with paging if that is a terminal. NULLCMD and READNULLCMD may refer to shell functions. COMMAND EXECUTIONIf 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 in the section `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. FUNCTIONSShell functions are defined with the function reserved word or the special syntax `funcname ()'. 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 the section `Command Execution'.)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. AUTOLOADING FUNCTIONSA function can be marked as undefined using the autoload builtin (or `functions -u' or `typeset -fu'). Such a function has no body. When the function is first executed, the shell searches for its definition using the elements of the fpath variable. Thus to define functions for autoloading, a typical sequence is:
The usual alias expansion during reading will be suppressed if the autoload builtin or its equivalent is given the option -U. This is recommended for the use of functions supplied with the zsh distribution. Note that for functions precompiled with the zcompile builtin command the flag -U must be provided when the .zwc file is created, as the corresponding information is compiled into the latter. For each element in fpath, the shell looks for three possible files, the newest of which is used to load the definition for the function:
In summary, the order of searching is, first, in the parents of directories in fpath for the newer of either a compiled directory or a directory in fpath; second, if more than one of these contains a definition for the function that is sought, the leftmost in the fpath is chosen; and third, within a directory, the newer of either a compiled function or an ordinary function definition is used. If the KSH_AUTOLOAD option is set, or the file contains only a simple definition of the function, the file's contents will be executed. This will normally define the function in question, but may also perform initialization, which is executed in the context of the function execution, and may therefore define local parameters. It is an error if the function is not defined by loading the file. Otherwise, the function body (with no surrounding `funcname() {...}') is taken to be the complete contents of the file. This form allows the file to be used directly as an executable shell script. If processing of the file results in the function being re-defined, the function itself is not re-executed. To force the shell to perform initialization and then call the function defined, the file should contain initialization code (which will be executed then discarded) in addition to a complete function definition (which will be retained for subsequent calls to the function), and a call to the shell function, including any arguments, at the end. For example, suppose the autoload file func contains
then `func; func' with KSH_AUTOLOAD set will produce both messages on the first call, but only the message `This is func' on the second and subsequent calls. Without KSH_AUTOLOAD set, it will produce the initialization message on the first call, and the other message on the second and subsequent calls. It is also possible to create a function that is not marked as autoloaded, but which loads its own definition by searching fpath, by using `autoload -X' within a shell function. For example, the following are equivalent:
and
In fact, the functions command outputs `builtin autoload -X' as the body of an autoloaded function. A true autoloaded function can be identified by the presence of the comment `# undefined' in the body, because all comments are discarded from defined functions. This is done so that
produces a reasonable result. To load the definition of an autoloaded function myfunc without executing myfunc, use:
SPECIAL FUNCTIONSThe following functions, if defined, have special meaning to the shell:
The functions beginning `TRAP' may alternatively be defined with the trap builtin: this may be preferable for some uses, as they are then run in the environment of the calling process, rather than in their own function environment. Apart from the difference in calling procedure and the fact that the function form appears in lists of functions, the forms
and
JOBSIf 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:
indicating that the job which was started asynchronously was job number 1 and had one (top-level) process, whose process ID was 1234. If a job is started with `&|' or `&!', then that job is immediately disowned. After startup, it does not have a place in the job table, and is not subject to the job control features described here. If you are running a job and wish to do something else you may hit the key ^Z (control-Z) which sends a TSTP signal to the current job: this key may be redefined by the susp option of the external stty command. The shell will then normally indicate that the job has been `suspended', and print another prompt. You can then manipulate the state of this job, putting it in the background with the bg command, or run some other commands and then eventually bring the job back into the foreground with the foreground command fg. A ^Z takes effect immediately and is like an interrupt in that pending output and unread input are discarded when it is typed. A job being run in the background will suspend if it tries to read from the terminal. Background jobs are normally allowed to produce output, but this can be disabled by giving the command `stty tostop'. If you set this tty option, then background jobs will suspend when they try to produce output like they do when they try to read input. When a command is suspended and continued later with the fg or wait builtins, zsh restores tty modes that were in effect when it was suspended. This (intentionally) does not apply if the command is continued via `kill -CONT', nor when it is continued with bg. There are several ways to refer to jobs in the shell. A job can be referred to by the process ID of any process of the job or by one of the following:
The shell learns immediately whenever a process changes state. It normally informs you whenever a job becomes blocked so that no further progress is possible. If the NOTIFY option is not set, it waits until just before it prints a prompt before it informs you. When the monitor mode is on, each background job that completes triggers any trap set for CHLD. When you try to leave the shell while jobs are running or suspended, you will be warned that `You have suspended (running) jobs'. You may use the jobs command to see what they are. If you do this or immediately try to exit again, the shell will not warn you a second time; the suspended jobs will be terminated, and the running jobs will be sent a SIGHUP signal, if the HUP option is set. To avoid having the shell terminate the running jobs, either use the nohup command (see nohup(1)) or the disown builtin. SIGNALSThe INT and QUIT signals for an invoked command are ignored if the command is followed by `&' and the MONITOR option is not active. Otherwise, signals have the values inherited by the shell from its parent (but see the TRAPNAL special functions in the section `Functions').ARITHMETIC EVALUATIONThe shell can perform integer and floating point arithmetic, either using the builtin let, or via a substitution of the form $((...)). For integers, the shell is usually compiled to use 8-byte precision where this is available, otherwise precision is 4 bytes. This can be tested, for example, by giving the command `print - $(( 12345678901 ))'; if the number appears unchanged, the precision is at least 8 bytes. Floating point arithmetic is always double precision.The let builtin command takes arithmetic expressions as arguments; each is evaluated separately. Since many of the arithmetic operators, as well as spaces, require quoting, an alternative form is provided: for any command which begins with a `((', all the characters until a matching `))' are treated as a quoted expression and arithmetic expansion performed as for an argument of let. More precisely, `((...))' is equivalent to `let "..."'. For example, the following statement
is equivalent to
both assigning the value 3 to the shell variable val and returning a zero status. Integers can be in bases other than 10. A leading `0x' or `0X' denotes hexadecimal. Integers may also be of the form `base#n', where base is a decimal number between two and thirty-six representing the arithmetic base and n is a number in that base (for example, `16#ff' is 255 in hexadecimal). The base# may also be omitted, in which case base 10 is used. For backwards compatibility the form `[base]n' is also accepted. It is also possible to specify a base to be used for output in the form `[#base]', for example `[#16]'. This is used when outputting arithmetical substitutions or when assigning to scalar parameters, but an explicitly defined integer or floating point parameter will not be affected. If an integer variable is implicitly defined by an arithmetic expression, any base specified in this way will be set as the variable's output arithmetic base as if the option `-i base' to the typeset builtin had been used. The expression has no precedence and if it occurs more than once in a mathematical expression, the last encountered is used. For clarity it is recommended that it appear at the beginning of an expression. As an example:
outputs first `8#40', the rightmost value in the given output base, and then `8#40 16#20', because y has been explicitly declared to have output base 16, while x (assuming it does not already exist) is implicitly typed by the arithmetic evaluation, where it acquires the output base 8. When an output base is specified using the `[#base]' syntax, an appropriate base prefix will be output if necessary, so that the value output is valid syntax for input. If the # is doubled, for example `[##16]', then no base prefix is output. Floating point constants are recognized by the presence of a decimal point or an exponent. The decimal point may be the first character of the constant, but the exponent character e or E may not, as it will be taken for a parameter name. An arithmetic expression uses nearly the same syntax, precedence, and associativity of expressions in C. The following operators are supported (listed in decreasing order of precedence):
The operators `&&', `||', `&&=', and `||=' are short-circuiting, and only one of the latter two expressions in a ternary operator is evaluated. Note the precedence of the bitwise AND, OR, and XOR operators. Mathematical functions can be called with the syntax `func(args)', where the function decides if the args is used as a string or a comma-separated list of arithmetic expressions. The shell currently defines no mathematical functions by default, but the module zsh/mathfunc may be loaded with the zmodload builtin to provide standard floating point mathematical functions. An expression of the form `##x' where x is any character sequence such as `a', `^A', or `\M-\C-x' gives the ASCII value of this character and an expression of the form `#foo' gives the ASCII value of the first character of the value of the parameter foo. Note that this is different from the expression `$#foo', a standard parameter substitution which gives the length of the parameter foo. `#\' is accepted instead of `##', but its use is deprecated. Named parameters and subscripted arrays can be referenced by name within an arithmetic expression without using the parameter expansion syntax. For example,
assigns twice the value of $val1 to the parameter named val2. An internal integer representation of a named parameter can be specified with the integer builtin. Arithmetic evaluation is performed on the value of each assignment to a named parameter declared integer in this manner. Assigning a floating point number to an integer results in rounding down to the next integer. Likewise, floating point numbers can be declared with the float builtin; there are two types, differing only in their output format, as described for the typeset builtin. The output format can be bypassed by using arithmetic substitution instead of the parameter substitution, i.e. `${float}' uses the defined format, but `$((float))' uses a generic floating point format. Promotion of integer to floating point values is performed where necessary. In addition, if any operator which requires an integer (`~', `&', `|', `^', `%', `<<', `>>' and their equivalents with assignment) is given a floating point argument, it will be silently rounded down to the next integer. Scalar variables can hold integer or floating point values at different times; there is no memory of the numeric type in this case. If a variable is first assigned in a numeric context without previously being declared, it will be implicitly typed as integer or float and retain that type either until the type is explicitly changed or until the end of the scope. This can have unforeseen consequences. For example, in the loop
if f has not already been declared, the first assignment will cause it to be created as an integer, and consequently the operation `f += 0.1' will always cause the result to be truncated to zero, so that the loop will fail. A simple fix would be to turn the initialization into `f = 0.0'. It is therefore best to declare numeric variables with explicit types. CONDITIONAL EXPRESSIONSA conditional expression is used with the [[ compound command to test attributes of files and to compare strings. Each expression can be constructed from one or more of the following unary or binary expressions:
Normal shell expansion is performed on the file, string and pattern arguments, but the result of each expansion is constrained to be a single word, similar to the effect of double quotes. However, pattern metacharacters are active for the pattern arguments; the patterns are the same as those used for filename generation, see zshexpn(1), but there is no special behaviour of `/' nor initial dots, and no glob qualifiers are allowed. In each of the above expressions, if file is of the form `/dev/fd/n', where n is an integer, then the test applied to the open file whose descriptor number is n, even if the underlying system does not support the /dev/fd directory. In the forms which do numeric comparison, the expressions exp undergo arithmetic expansion as if they were enclosed in $((...)). For example, the following:
tests if either file foo or file bar exists, and if so, if the value of the parameter report begins with `y'; if the complete condition is true, the message `File exists.' is printed. PROMPT EXPANSIONPrompt sequences undergo a special form of expansion. This type of expansion is also available using the -P option to the print builtin.If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic expansion. See zshexpn(1). Certain escape sequences may be recognised in the prompt string. If the PROMPT_BANG option is set, a `!' in the prompt is replaced by the current history event number. A literal `!' may then be represented as `!!'. If the PROMPT_PERCENT option is set, certain escape sequences that start with `%' are expanded. Some escapes take an optional integer argument, which should appear between the `%' and the next character of the sequence. The following escape sequences are recognized:
Index
This document was created by man2html, using the manual pages. Time: 05:45:56 GMT, April 19, 2024 |