|
Go to the previous, next chapter.
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.
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.
Any arguments are ignored, except for a lone --help
or --version (see section Common options).
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 be faster.
Any arguments are ignored, except for a lone --help
or --version (see section Common options).
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 Common options. A single
non-option argument is also allowed: test returns
true if the argument is not null.
These options test for particular types of files.
(Everything's a file, but not all files are the same!)
-b file - -b - True if file
exists and is a block special device.
-c file - -c - True if file
exists and is a character special device.
-d file - -d - True if file
exists and is a directory.
-f file - -f - True if file
exists and is a regular file.
-h file - -L file
- -L - -h - True if file exists
and is a symbolic link.
-p file - -p - True if file
exists and is a named pipe.
-S file - -S - True if file
exists and is a socket.
-t [ fd ] - -t
- True if fd is opened on a terminal. If fd
is omitted, it defaults to 1 (standard output).
These options test for particular access permissions.
-g file - -g - True if file
exists and has its set-group-id bit set.
-k file - -k - True if file
has its sticky bit set.
-r file - -r - True if file
exists and is readable.
-u file - -u - True if file
exists and has its set-user-id bit set.
-w file - -w - True if file
exists and is writable.
-x file - -x - True if file
exists and is executable.
-O file - -O - True if file
exists and is owned by the current effective user id.
-G file - -G - True if file
exists and is owned by the current effective group id.
These options test other file characteristics.
-e file - -e - True if file
exists.
-s file - -s - True if file
exists and has a size greater than zero.
file1 -nt file2
- -nt - True if file1 is newer
(according to modification date) than file2.
file1 -ot file2
- -ot - True if file1 is older
(according to modification date) than file2.
file1 -ef file2
- -ef - True if file1 and file2
have the same device and inode numbers, i.e., if they are
hard links to each other.
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.
-z string - -z - True
if the length of string is zero.
-n string - string
- -n - True if the length of string is
non-zero.
string1 = string2
- = - True if the strings are equal.
string1 != string2
- != - True if the strings are not equal.
Numeric relationals. The arguments must be entirely numeric
(possibly negative), or the special expression -l string,
which evaluates to the length of string.
arg1 -eq arg2
- arg1 -ne arg2
- arg1 -lt arg2
- arg1 -le arg2
- arg1 -gt arg2
- arg1 -ge arg2
- -eq - -ne - -lt - -le - -gt
- -ge - These arithmetic binary operators return
true if arg1 is equal, not-equal, less-than,
less-than-or-equal, greater-than, or
greater-than-or-equal than arg2, respectively.
For example:
test -1 -gt -2 && echo yes
@result{} yes
test -l abc -gt 1 && echo yes
@result{} yes
test 0x100 -eq 1
@error{} test: integer expression expected before -eq
The usual logical connectives.
! expr - ! - True if expr
is false.
expr1 -a expr2
- -a - True if both expr1 and expr2
are true.
expr1 -o expr2
- -o - True if either expr1 or expr2
is true.
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
coerces 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, though you may
need to quote them to protect characters with special meaning to
the shell, e.g., spaces.
Operators may 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).
Exit status:
0 if the expression is neither null nor 0,
1 if the expression is null or 0,
2 for invalid expressions.
The usual logical connectives and relations, in order of
precedence.
| - Yields its first argument if it is neither
null nor 0, otherwise its second argument.
& - Yields its first argument if neither
argument is null or 0, otherwise 0.
= > - Compare the arguments and return 1 if
the relation is true, 0 otherwise. == is a
synonym for =. expr first tries
to coerce both arguments to numbers and do a numeric
comparison; if either coercion fails, it does a
lexicographic comparison.
Numeric operators, in order of increasing precedence. The
connectives (previous section) have higher precedence, the string
operators (following section) have lower.
+ - - Addition and subtraction. Both arguments
are coerced to numbers; an error occurs if this cannot be
done.
* / % - Multiplication, division, remainder.
Both arguments are coerced to numbers; an error occurs if
this cannot be done.
String operators. These have lowest precedence.
string : regex
- Perform pattern matching. The arguments are coerced to
strings and the second is considered to be a (basic, a la 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.
See section 'Regular Expression Library' in Regex,
for details of regular expression syntax.
match string regex
- An alternative way to do pattern matching. This is the
same as string : regex.
substr string position length
- Returns the substring of string beginning at position
with length at most length. If either position
or length is negative or non-numeric, returns
the null string.
index string character-class
- Returns the first position in string where
the first character in charset was found. If
no character in charset is found in string,
return 0.
length string - Returns the
length of string.
The keywords cannot be used as strings.
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
expr abc : 'a\(.\)c'
@result{} b
expr index abcdef cz
@result{} 3
expr index index a
@error{} expr: syntax error
To return to the Ready-to-Run Software Win95Pak Table of Contents please press here.
|