POSH(1) | User commands | POSH(1) |
posh - Policy-compliant Ordinary SHell
posh [+-aCefilnuvx] [+-o option] [[-c command-string [command-name] | -s | file ] [argument...]]
posh is a reimplementation of the Bourne shell, a command interpreter for both interactive and script use.
The following options can be specified only on the command line:
-c command-string
-i
-l
-s
In addition to the above, the options described in the set builtin command can also be used on the command line.
If neither the -c nor the -s options are specified, the first non-option argument specifies the name of a file the shell reads commands from; if there are no non-option arguments, the shell reads commands from standard input. The name of the shell (i.e., the contents of the $0) parameter is determined as follows: if the -c option is used and there is a non-option argument, it is used as the name; if commands are being read from a file, the file is used as the name; otherwise the name the shell was called with (i.e., argv[0]) is used.
A shell is interactive if the -i option is used or if both standard input and standard error are attached to a tty. An interactive shell has job control enabled (if available), ignores the INT, QUIT and TERM signals, and prints prompts before reading input (see PS1 and PS2 parameters).
A shell is privileged if the -p option is used or if the real user-id or group-id does not match the effective user-id or group-id (see getuid(2), getgid(2)). A privileged shell does not process $HOME/.profile. Instead the file /etc/suid_profile is processed. Clearing the privileged option causes the shell to set its effective user-id (group-id) to its real user-id (group-id).
If the basename of the name the shell is called with (i.e., argv[0]) starts with - or if the -l option is used, the shell is assumed to be a login shell and the shell reads and executes the contents of /etc/profile and $HOME/.profile if they exist and are readable.
The exit status of the shell is 127 if the command file specified on the command line could not be opened, or non-zero if a fatal syntax error occurred during the execution of a script. In the absence of fatal errors, the exit status is that of the last command executed, or zero, if no command is executed.
The shell begins parsing its input by breaking it into words. Words, which are sequences of characters, are delimited by unquoted white-space characters (space, tab and newline) or meta-characters (<, >, |, ;, &, ( and )). Aside from delimiting words, spaces and tabs are ignored, while newlines usually delimit commands. The meta-characters are used in building the following tokens: <, <&, <<, >, >&, >>, etc. are used to specify redirections (see Input/Output Redirection below); | is used to create pipelines; ; is used to separate commands; & is used to create asynchronous pipelines; && and || are used to specify conditional execution; ;; is used in case statements; and lastly, ( ... ) are used to create subshells.
White-space and meta-characters can be quoted individually using backslash (\), or in groups using double (") or single (') quotes. Note that the following characters are also treated specially by the shell and must be quoted if they are to represent themselves: \, ", ', #, $, `, ~, {, }, *, ? and [. The first three of these are the above mentioned quoting characters (see Quoting below); #, if used at the beginning of a word, introduces a comment — everything after the # up to the nearest newline is ignored; $ is used to introduce parameter, command and arithmetic substitutions (see Substitution below); ` introduces an old-style command substitution (see Substitution below); ~ begins a directory expansion (see Tilde Expansion below); { and } delimit csh(1) style alternations (see Brace Expansion below); and, finally, *, ? and [ are used in file name generation (see File Name Patterns below).
As words and tokens are parsed, the shell builds commands, of which there are two basic types: simple-commands, typically programs that are executed, and compound-commands, such as for and if statements, grouping constructs and function definitions.
A simple-command consists of some combination of parameter assignments (see Parameters below), input/output redirections (see Input/Output Redirections below), and command words; the only restriction is that parameter assignments come before any command words. The command words, if any, define the command that is to be executed and its arguments. The command may be a shell built-in command, a function or an external command, i.e., a separate executable file that is located using the PATH parameter (see Command Execution below). Note that all command constructs have an exit status: for external commands, this is related to the status returned by wait(2) (if the command could not be found, the exit status is 127, if it could not be executed, the exit status is 126); the exit status of other command constructs (built-in commands, functions, compound-commands, pipelines, lists, etc.) are all well defined and are described where the construct is described. The exit status of a command consisting only of parameter assignments is that of the last command substitution performed during the parameter assignment or zero if there were no command substitutions.
Commands can be chained together using the | token to form pipelines, in which the standard output of each command but the last is piped (see pipe(2)) to the standard input of the following command. The exit status of a pipeline is that of its last command. A pipeline may be prefixed by the ! reserved word which causes the exit status of the pipeline to be logically complemented: if the original status was 0 the complemented status will be 1, and if the original status was not 0, then the complemented status will be 0.
Lists of commands can be created by separating pipelines by any of the following tokens: &&, ||, &, |& and ;. The first two are for conditional execution: cmd1 && cmd2 executes cmd2 only if the exit status of cmd1 is zero; || is the opposite — cmd2 is executed only if the exit status of cmd1 is non-zero. && and || have equal precedence which is higher than that of &, |& and ;, which also have equal precedence. The & token causes the preceding command to be executed asynchronously, that is, the shell starts the command, but does not wait for it to complete (the shell does keep track of the status of asynchronous commands — see Job Control below). When an asynchronous command is started when job control is disabled (i.e., in most scripts), the command is started with signals INT and QUIT ignored and with input redirected from /dev/null (however, redirections specified in the asynchronous command have precedence). Note that a command must follow the && and || operators, while a command need not follow &, |& and ;. The exit status of a list is that of the last command executed, with the exception of asynchronous lists, for which the exit status is 0.
Compound commands are created using the following reserved words — these words are only recognized if they are unquoted and if they are used as the first word of a command (i.e., they can't be preceded by parameter assignments or redirections):
case | else | |||
do | esac | if | time | [[ |
done | fi | in | until | { |
elif | for | select | while | } |
Note: Some shells (but not this one) execute control structure commands in a subshell when one or more of their file descriptors are redirected, so any environment changes inside them may fail. To be portable, the exec statement should be used instead to redirect file descriptors before the control structure.
In the following compound command descriptions, command lists (denoted as list) that are followed by reserved words must end with a semi-colon, a newline or a (syntactically correct) reserved word. For example,
{ echo foo; echo bar; }
{ echo foo; echo bar<newline>}
{ { echo foo; echo bar; } } are all valid, but
{ echo foo; echo bar } is not.
( list )
{ list }
case word in [ [(] pattern [| pattern] ... ) list ;; ] ... esac
for name [ in word ... term ] do list done
if list then list [elif list then list] ... [else list] fi
until list do list done
while list do list done
name () command
time [ -p ] [ pipeline ]
Quoting is used to prevent the shell from treating characters or words specially. There are three methods of quoting: First, \ quotes the following character, unless it is at the end of a line, in which case both the \ and the newline are stripped. Second, a single quote (') quotes everything up to the next single quote (this may span lines). Third, a double quote (") quotes all characters, except $, ` and \, up to the next unquoted double quote. $ and ` inside double quotes have their usual meaning (i.e., parameter, command or arithmetic substitution) except no field splitting is carried out on the results of double-quoted substitutions. If a \ inside a double-quoted string is followed by \, $, ` or ", it is replaced by the second character; if it is followed by a newline, both the \ and the newline are stripped; otherwise, both the \ and the character following are unchanged.
Note: see POSIX Mode below for a special rule regarding sequences of the form "...`...\"...`..".
The first step the shell takes in executing a simple-command is to perform substitutions on the words of the command. There are three kinds of substitution: parameter, command and arithmetic. Parameter substitutions, which are described in detail in the next section, take the form $name or ${...}; command substitutions take the form $(command) or `command`; and arithmetic substitutions take the form $((expression)).
If a substitution appears outside of double quotes, the results of the substitution are generally subject to word or field splitting according to the current value of the IFS parameter. The IFS parameter specifies a list of characters which are used to break a string up into several words; any characters from the set space, tab and newline that appear in the IFS characters are called IFS white space. Sequences of one or more IFS white space characters, in combination with zero or one non-IFS white space characters delimit a field. As a special case, leading and trailing IFS white space is stripped (i.e., no leading or trailing empty field is created by it); leading or trailing non-IFS white space does create an empty field. Example: if IFS is set to `<space>:', the sequence of characters `<space>A<space>:<space><space>B::D' contains four fields: `A', `B', `' and `D'. Note that if the IFS parameter is set to the null string, no field splitting is done; if the parameter is unset, the default value of space, tab and newline is used.
The results of substitution are, unless otherwise specified, also subject to brace expansion and file name expansion (see the relevant sections below).
A command substitution is replaced by the output generated by the specified command, which is run in a subshell. For $(command) substitutions, normal quoting rules are used when command is parsed, however, for the `command` form, a \ followed by any of $, ` or \ is stripped (a \ followed by any other character is unchanged). As a special case in command substitutions, a command of the form < file is interpreted to mean substitute the contents of file ($(< foo) has the same effect as $(cat foo), but it is carried out more efficiently because no process is started).
NOTE: $(command) expressions are currently parsed by finding the matching parenthesis, regardless of quoting. This will hopefully be fixed soon.
Arithmetic substitutions are replaced by the value of the specified expression. For example, the command echo $((2+3*4)) prints 14. See Arithmetic Expressions for a description of an expression.
Parameters are shell variables; they can be assigned values and their values can be accessed using a parameter substitution. A parameter name is either one of the special single punctuation or digit character parameters described below, or a letter followed by zero or more letters or digits (`_' counts as a letter). The later form can be treated as arrays by appending an array index of the form: [expr] where expr is an arithmetic expression. Array indices are currently limited to the range 0 through 1023, inclusive. Parameter substitutions take the form $name, ${name} or ${name[expr]}, where name is a parameter name. If substitution is performed on a parameter (or an array parameter element) that is not set, a null string is substituted unless the nounset option (set -o nounset or set -u) is set, in which case an error occurs.
Parameters can be assigned values in a number of ways. First, the shell implicitly sets some parameters like #, PWD, etc.; this is the only way the special single character parameters are set. Second, parameters are imported from the shell's environment at startup. Third, parameters can be assigned values on the command line, for example, `FOO=bar' sets the parameter FOO to bar; multiple parameter assignments can be given on a single command line and they can be followed by a simple-command, in which case the assignments are in effect only for the duration of the command (such assignments are also exported, see below for implications of this). Note that both the parameter name and the = must be unquoted for the shell to recognize a parameter assignment. The fourth way of setting a parameter is with the export and readonly commands; see their descriptions in the Command Execution section. Fifth, for and select loops set parameters as well as the getopts, read and set -A commands. Lastly, parameters can be assigned values using assignment operators inside arithmetic expressions (see Arithmetic Expressions below) or using the ${name=value} form of parameter substitution (see below).
Parameters with the export attribute (set using the export command, or by parameter assignments followed by simple commands) are put in the environment (see environ(5)) of commands run by the shell as name=value pairs. The order in which parameters appear in the environment of a command is unspecified. When the shell starts up, it extracts parameters and their values from its environment and automatically sets the export attribute for those parameters.
Modifiers can be applied to the ${name} form of parameter substitution:
${name:-word}
${name:+word}
${name:=word}
${name:?word}
In the above modifiers, the : can be omitted, in which case the conditions only depend on name being set (as opposed to set and not null). If word is needed, parameter, command, arithmetic and tilde substitution are performed on it; if word is not needed, it is not evaluated.
The following forms of parameter substitution can also be used:
${#name}
${#name[*]}, ${#name[@]}
${name#pattern}, ${name##pattern}
${name%pattern}, ${name%%pattern}
The following special parameters are implicitly set by the shell and cannot be set directly using assignments:
!
#
$
-
?
0
1 ... 9
*
@
The following parameters are set and/or used by the shell:
"CDPATH"
COLUMNS
ERRNO
Not implemented yet.
EXECSHELL
FCEDIT
FPATH
HOME
IFS
Note: this parameter is not imported from the environment when the shell is started.
POSH_VERSION
LINENO
LINES
Not implemented yet.
OLDPWD
OPTARG
OPTIND
PATH
PPID
PS1
PS2
PS4
PWD
REPLY
TMPDIR
Tilde expansion, which is done in parallel with parameter substitution, is done on words starting with an unquoted ~. The characters following the tilde, up to the first /, if any, are assumed to be a login name. If the login name is empty, + or -, the value of the HOME, PWD, or OLDPWD parameter is substituted, respectively. Otherwise, the password file is searched for the login name, and the tilde expression is substituted with the user's home directory. If the login name is not found in the password file or if any quoting or parameter substitution occurs in the login name, no substitution is performed.
In parameter assignments (those preceding a simple-command or those occurring in the arguments of alias, export, and readonly, tilde expansion is done after any unquoted colon (:), and login names are also delimited by colons.
The home directory of previously expanded login names are cached and re-used. The alias -d command may be used to list, change and add to this cache (e.g., `alias -d fac=/usr/local/facilities; cd ~fac/bin').
A file name pattern is a word containing one or more unquoted ? or * characters or [..] sequences. Once brace expansion has been performed, the shell replaces file name patterns with the sorted names of all the files that match the pattern (if no files match, the word is left unchanged). The pattern elements have the following meaning:
"?"
*
[..]
[!..]
Note that posh currently never matches . and .., but the original ksh, Bourne sh and bash do, so this may have to change (too bad).
Note that none of the above pattern elements match either a period (.) at the start of a file name or a slash (/), even if they are explicitly used in a [..] sequence; also, the names . and .. are never matched, even by the pattern .*.
The POSIX character classes (i.e., [:class-name:] inside a [..] expression) are not yet implemented.
When a command is executed, its standard input, standard output and standard error (file descriptors 0, 1 and 2, respectively) are normally inherited from the shell. Three exceptions to this are commands in pipelines, for which standard input and/or standard output are those set up by the pipeline, asynchronous commands created when job control is disabled, for which standard input is initially set to be from /dev/null, and commands for which any of the following redirections have been specified:
"> file"
">| file"
">> file"
"< file"
"<> file"
"<< marker"
"<<- marker"
"<& fd"
">& fd"
In any of the above redirections, the file descriptor that is redirected (i.e., standard input or standard output) can be explicitly given by preceding the redirection with a single digit. Parameter, command and arithmetic substitutions, tilde substitutions and (if the shell is interactive) file name generation are all performed on the file, marker and fd arguments of redirections. Note however, that the results of any file name generation are only used if a single file is matched; if multiple files match, the word with the unexpanded file name generation characters is used.
For simple-commands, redirections may appear anywhere in the command, for compound-commands (if statements, etc.), any redirections must appear at the end. Redirections are processed after pipelines are created and in the order they are given, so
cat /foo/bar 2>&1 > /dev/null | cat -n will print an error with a line number prepended to it.
Integer arithmetic expressions can be used inside $((..)) expressions, inside array references (e.g., name[expr]), as numeric arguments to the test command, and as the value of an assignment to an integer parameter.
Expression may contain alpha-numeric parameter identifiers, array references, and integer constants and may be combined with the following C operators (listed and grouped in increasing order of precedence).
Unary operators:
Binary operators:
= *= /= %= += -= <<= >>= &= ^= |=
||
&&
|
^
&
== !=
< <= >= >
<< >>
+ -
* / %
Ternary operator:
Grouping operators:
Integer constants may be specified with arbitrary bases using the notation base#number, where base is a decimal integer specifying the base, and number is a number in the specified base.
The operators are evaluated as follows:
"unary +"
"unary -"
"!"
"~"
"++"
"++"
","
"="
"*= /= %= += -= <<= >>= &= ^= |="
"||"
"&&"
"|"
"^"
"&"
"=="
"!="
"<"
"<= >= >"
"<< >>"
"+ - * /"
"%"
"<arg1> ? <arg2> : <arg3>"
Functions are defined using the Bourne/POSIX shell name() syntax. Functions are like .-scripts in that they are executed in the current environment, however, unlike .-scripts, shell arguments (i.e., positional parameters, $1, etc.) are never visible inside them. When the shell is determining the location of a command, functions are searched after special built-in commands, and before regular and non-regular built-ins, and before the PATH is searched.
An existing function may be deleted using unset -f function-name.
Since functions are executed in the current shell environment, parameter assignments made inside functions are visible after the function completes.
The exit status of a function is that of the last command executed in the function. A function can be made to finish immediately using the return command; this may also be used to explicitly specify the exit status.
After evaluation of command line arguments, redirections and parameter assignments, the type of command is determined: a special built-in, a function, a regular built-in or the name of a file to execute found using the PATH parameter. The checks are made in the above order. Special built-in commands differ from other commands in that the PATH parameter is not used to find them, an error during their execution can cause a non-interactive shell to exit and parameter assignments that are specified before the command are kept after the command completes. Just to confuse things, if the posix option is turned off (see set command below) some special commands are very special in that no field splitting, file globbing, brace expansion nor tilde expansion is performed on arguments that look like assignments. Regular built-in commands are different only in that the PATH parameter is not used to find them.
Job control refers to the shell's ability to monitor and control jobs, which are processes or groups of processes created for commands or pipelines. At a minimum, the shell keeps track of the status of the background (i.e., asynchronous) jobs that currently exist; this information can be displayed using the jobs command. If job control is fully enabled (using set -m or set -o monitor), as it is for interactive shells, the processes of a job are placed in their own process group, foreground jobs can be stopped by typing the suspend character from the terminal (normally ^Z), jobs can be restarted in either the foreground or background, using the fg and bg commands, respectively, and the state of the terminal is saved or restored when a foreground job is stopped or restarted, respectively.
Note that only commands that create processes (e.g., asynchronous commands, subshell commands, and non-built-in, non-function commands) can be stopped; commands like read cannot be.
When a job is created, it is assigned a job-number. For interactive shells, this number is printed inside [..], followed by the process-ids of the processes in the job when an asynchronous command is run. A job may be referred to in bg, fg, jobs, kill and wait commands either by the process id of the last process in the command pipeline (as stored in the $! parameter) or by prefixing the job-number with a percent sign (%). Other percent sequences can also be used to refer to jobs:
%+ | %+ | The most recently stopped job, or, if there are no stopped jobs, the oldest running job. | |
%%, % | %%, % | Same as %+. | |
%- | %- | The job that would be the %+ job, if the later did not exist. | |
%n | %n | The job with job-number n. | |
%?string | %?string | The job containing the string string (an error occurs if multiple jobs are matched). | |
%string | %string | The job starting with string string (an error occurs if multiple jobs are matched). |
When a job changes state (e.g., a background job finishes or foreground job is stopped), the shell prints the following status information: [number] flag status command where
" number"
" flag"
" status"
"Running"
"Done [(number)]"
"Stopped [(signal)]"
"signal-description [(core dumped)]"
" command"
When an attempt is made to exit the shell while there are jobs in the stopped state, the shell warns the user that there are stopped jobs and does not exit. If another attempt is immediately made to exit the shell, the stopped jobs are sent a HUP signal and the shell exits. Similarly, if the nohup option is not set and there are running jobs when an attempt is made to exit a login shell, the shell warns the user and does not exit. If another attempt is immediately made to exit the shell, the running jobs are sent a HUP signal and the shell exits.
posh implements the following builtin utilities:
~/.profile /etc/profile /etc/suid_profile
Any bugs in posh should be reported via the Debian BTS. Legitimate bugs are inconsistencies between manpage and behavior, and inconsistencies between behavior and Debian policy (currently SUSv3 compliance with the following exceptions: echo -n, binary -a and -o to test, local scoping).
This page documents the Policy-compliant Ordinary SHell.
This shell is based on pdksh.
awk(1), ksh(1), dash(1), ed(1), getconf(1), getopt(1), sed(1), stty(1), vi(1), dup(2), execve(2), getgid(2), getuid(2), open(2), pipe(2), wait(2), getopt(3), rand(3), signal(3), system(3), environ(5)
01/24/2011 | User commands |