If a simple command is preceded by the word exec, it is executed in the parent shell without forking. If preceded by command, the command word is taken to be the name of an external command, rather than a shell function or builtin. If preceded by noglob, filename generation is not performed on any of the words. If preceded by a -, the command is executed with a - prepended to its argv[0] string. If preceded by nocorrect, spelling correction is not done on any of the words.
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.
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.
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.
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 one 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 a &, the shell executes it in the background, and does not wait for it to finish.
A complex command is one of the following:
- for name [ in word ... ]
- do list
- done
- 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.
- for name [ in word ... ] ; sublist
- This is a shorthand for for. Though it may cause confusion, it is included for convenience; its use in scripts is discouraged, unless sublist is a command of the form { list }.
- foreach name ( word ... )
- list
- end
- Another form of for.
- for name in word ...
- {
- list
- }
- Another form of for.
- for name ( word ... ) {
- list
- }
- Another form of for: this requires the option CSH_JUNKIE_PAREN.
- for name ( word ... ) sublist
- Another form of for: this also requires CSH_JUNKIE_PAREN.
- select name [ in word ... ]
- do list
- done
- 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.
- select name [ in word ] ; sublist
- A short form of select.
- case word in [ 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.
- case word { [ pattern ) list ;; ] ... }
- Another form of case.
- 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.
- if ( list ) sublist
- A short form of if: this requires the option CSH_JUNKIE_PAREN.
- if ( list ) {
- list
- } elif ( list ) {
- list
- } ... else {
- list
- }
- An alternative form of if. The parentheses surrounding list can be omitted if the only command in the list is a conditional expression of the form [[ exp ]] (see below). This form also requires CSH_JUNKIE_PAREN.
- while list
- do list
- done
- Execute the do list as long as the while list returns a zero exit status.
- while ( list ) {
- list
- }
- An alternative form of while: this requires the option CSH_JUNKIE_PAREN.
- 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.
- repeat word sublist
- This is a short form of repeat.
- ( list )
- Execute list in a subshell.
- { list }
- Execute list.
- function word [ ( ) ] ... { list }
- word ... ( ) { list }
- word ... ( ) sublist
- 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.
do done esac then elif else fi for case if while function repeat time until exec command select coproc noglob - nocorrect foreach end
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. \foo. But there is nothing to prevent an alias being defined for \foo as well.
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 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). 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 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). 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 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.
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.
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:
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:
[1] 1234
indicating that the job which was started asynchronously was job number 1 and had one (top-level) process, whose process id was 1234.
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. 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.
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 notify 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. To avoid having the shell terminate the running jobs, either use the nohup(1) command or the disown builtin (see below).
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):
- + - ! ++ - -
- unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement
- << >>
- bitwise shift left, right
- &
- bitwise AND
- ^
- bitwise XOR
- |
- bitwise OR
- **
- exponentiation
- * / %
- multiplication, division, modulus (remainder)
- + -
- addition, subtraction
- < > <= >=
- comparison
- == !=
- equality and inequality
- &&
- logical AND
- | | ^^
- logical OR, XOR
- ? :
- ternary operator
- = += -= *= /= %= &= ^= |= <<= >>= &&= | |= ^^= **=
- assignment
- ,
- comma operator
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.
An expression of the form #\x where x is any character 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.
Named parameters and subscripted arrays can be referenced by name within an arithmetic expression without using the parameter substitution syntax.
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.
Since many of the arithmetic operators require quoting, an alternative form of the let command is provided. For any command which begins with a ((, all the characters until a matching )) are treated as a quoted expression. More precisely, ((...)) is equivalent to let "...".
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.