| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes commands that are primarily useful for their exit
status, rather than their output. Thus, they are often used as the
condition of shell if statements, or as the last command in a
pipeline.
16.1 false: Do nothing, unsuccessfullyDo nothing, unsuccessfully. 16.2 true: Do nothing, successfullyDo nothing, successfully. 16.3 test: Check file types and compare valuesCheck file types and compare values. 16.4 expr: Evaluate expressionsEvaluate expressions.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
false: Do nothing, unsuccessfully
false does nothing except return an exit status of 1, meaning
failure. It can be used as a place holder in shell scripts
where an unsuccessful command is needed.
By default, false honors the `--help' and `--version'
options. However, that is contrary to POSIX, so when the environment
variable POSIXLY_CORRECT is set, false ignores all
command line arguments, including `--help' and `--version'.
This version of false is implemented as a C program, and is thus
more secure and faster than a shell script implementation, and may safely
be used as a dummy shell for the purpose of disabling accounts.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
true: Do nothing, successfully
true does nothing except return an exit status of 0, meaning
success. It can be used as a place holder in shell scripts
where a successful command is needed, although the shell built-in
command : (colon) may do the same thing faster.
In most modern shells, true is a built-in command, so when
you use `true' in a script, you're probably using the built-in
command, not the one documented here.
By default, true honors the `--help' and `--version'
options. However, that is contrary to POSIX, so when the environment
variable POSIXLY_CORRECT is set, true ignores all
command line arguments, including `--help' and `--version'.
This version of true is implemented as a C program, and is thus
more secure and faster than a shell script implementation, and may safely
be used as a dummy shell for the purpose of disabling accounts.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
test: Check file types and compare values
test returns a status of 0 (true) or 1 (false) depending on the
evaluation of the conditional expression expr. Each part of the
expression must be a separate argument.
test has file status checks, string operators, and numeric
comparison operators.
Because most shells have a built-in command by the same name, using the unadorned command name in a script or interactively may get you different functionality than that described here.
Besides the options below, test accepts a lone `--help' or
`--version'. See section 2. Common options. A single non-option argument
is also allowed: test returns true if the argument is not null.
16.3.1 File type tests -[bcdfhLpSt] 16.3.2 Access permission tests -[gkruwxOG] 16.3.3 File characteristic tests -e -s -nt -ot -ef 16.3.4 String tests -z -n = != 16.3.5 Numeric tests -eq -ne -lt -le -gt -ge 16.3.6 Connectives for test! -a -o
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options test for particular types of files. (Everything's a file, but not all files are the same!)
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options test for particular access permissions.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options test other file characteristics.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options test string characteristics. Strings are not quoted for
test, though you may need to quote them to protect characters
with special meaning to the shell, e.g., spaces.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Numeric relationals. The arguments must be entirely numeric (possibly
negative), or the special expression -l string, which
evaluates to the length of string.
For example:
test -1 -gt -2 && echo yes => yes test -l abc -gt 1 && echo yes => yes test 0x100 -eq 1 error--> test: integer expression expected before -eq |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
test The usual logical connectives.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
expr: Evaluate expressions
expr evaluates an expression and writes the result on standard
output. Each token of the expression must be a separate argument.
Operands are either numbers or strings. expr converts
anything appearing in an operand position to an integer or a string
depending on the operation being applied to it.
Strings are not quoted for expr itself, though you may need to
quote them to protect characters with special meaning to the shell,
e.g., spaces.
Operators may be given as infix symbols or prefix keywords. Parentheses may be used for grouping in the usual manner (you must quote parentheses to avoid the shell evaluating them, however).
0 if the expression is neither null nor 0, 1 if the expression is null or 0, 2 for invalid expressions. |
16.4.1 String expressions + : match substr index length 16.4.2 Numeric expressions + - * / % 16.4.3 Relations for expr| & < <= = == != >= > 16.4.4 Examples of using exprExamples.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
expr supports pattern matching and other string operators. These
have lower precedence than both the numeric and relational operators (in
the next sections).
grep) regular
expression, with a ^ implicitly prepended. The first argument is
then matched against this regular expression.
If the match succeeds and regex uses `\(' and `\)', the
: expression returns the part of string that matched the
subexpression; otherwise, it returns the number of characters matched.
If the match fails, the : operator returns the null string if
`\(' and `\)' are used in regex, otherwise 0.
Only the first `\( ... \)' pair is relevant to the return value; additional pairs are meaningful only for grouping the regular expression operators.
In the regular expression, \+, \?, and \| are
operators which respectively match one or more, zero or one, or separate
alternatives. SunOS and other expr's treat these as regular
characters. (POSIX allows either behavior.)
See section `Regular Expression Library' in Regex, for details of
regular expression syntax. Some examples are in 16.4.4 Examples of using expr.
/.
This makes it possible to test expr length + "$x" or
expr + "$x" : '.*/\(.\)' and have it do the right thing even if
the value of $x happens to be (for example) / or index.
This operator is a GNU extension. Portable shell scripts should use
" $token" : ' \(.*\)' instead of + "$token".
To make expr interpret keywords as strings, you must use the
quote operator.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
expr supports the usual numeric operators, in order of increasing
precedence. The string operators (previous section) have lower precedence,
the connectives (next section) have higher.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
expr
expr supports the usual logical connectives and relations. These
are higher precedence than either the string or numeric operators
(previous sections). Here is the list, lowest-precedence operator first.
== is a synonym for =. expr first tries to convert
both arguments to numbers and do a numeric comparison; if either
conversion fails, it does a lexicographic comparison using the character
collating sequence specified by the LC_COLLATE locale.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
expr Here are a few examples, including quoting for shell metacharacters.
To add 1 to the shell variable foo, in Bourne-compatible shells:
foo=`expr $foo + 1` |
To print the non-directory part of the file name stored in
$fname, which need not contain a /.
expr $fname : '.*/\(.*\)' '|' $fname |
An example showing that \+ is an operator:
expr aaa : 'a\+' => 3 |
expr abc : 'a\(.\)c' => b expr index abcdef cz => 3 expr index index a error--> expr: syntax error expr index quote index a => 0 |
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |