test: Evaluate a conditional expression

Evaluate a conditional expression expr.
Syntax
test expr [ expr

Options
Each operator and operand must be a separate argument. When the [
form is used, the last argument to the command must be a ]. Expressions
may be combined using the following operators, listed in decreasing order of
precedence.



! expr


True if expr is false.

( expr )

Returns the value of expr. This may be used to override the
normal precedence of operators.

expr1 -a expr2

True if both expr1 and expr2 are true.

expr1 -o expr2

True if either expr1 or expr2 is true.


The test and [ builtins evaluate
conditional expressions using a set of rules based on the number of arguments.




0 arguments

The expression is false.
1 argument

The expression is true if and only if the argument
is not null.
2 arguments

If the first argument is `!?, the expression
is true if and only if the second argument is null. If the first argument
is one of the unary conditional operators, the expression is true if
the unary test is true. If the first argument is not a valid unary operator,
the expression is false.
3 arguments

If the second argument is one of the binary conditional
operators, the result of the expression is the result of the binary
test using the first and third arguments as operands. If the first argument
is `!?, the value is the negation of the two-argument test
using the second and third arguments. If the first argument is exactly
`(? and the third argument is exactly `)?,
the result is the one-argument test of the second argument. Otherwise,
the expression is false. The `-a? and `-o?
operators are considered binary operators in this case.
4 arguments

If the first argument is `!?, the result
is the negation of the three-argument expression composed of the remaining
arguments. Otherwise, the expression is parsed and evaluated according
to precedence using the rules listed above.
5 or more arguments

The expression is parsed and evaluated according
to precedence using the rules listed above.



The above applies to the BOURNE shell built-in, the BASH `test?
command has the following additional options:
File type tests

These options test for particular types of files. All cases will only return
True (0) if the file exists.
`-b file?
True if file is a Block special device.

`-c file?
True if file is a Character special device.

`-d file?
True if file is a Directory.

`-e file?
True if file Exists.

`-f file?
True if file is a regular File.

`-g file?
True if file has its set-group-id bit set.

`-G file?
True if file is owned by the current effective group id.

`-k file?
True if file has its "sticky" bit set.

`-h file?
`-L file?
True if file is a symbolic Link.

`-O file?
True if file is owned by the current effective user id.

`-p file?
True if file is a named Pipe.

`-r file?
True if file is readable.

`-S file?
True if file is a Socket.

`-s file?
True if file has a Size greater than zero.

`-t [FD]?
True if FD is opened on a terminal. If FD is omitted, it defaults
to 1 (standard output).

`-u file?
True if file has its set-user-id bit set.

`-w file?
True if file is writable.

`-x file?
True if file is executable.

`file1 -ef file2?
True if file1 and file2 have the same device and inode numbers,
i.e., if they are hard links to each other.
File Age
These options test the file modification date.

`file1 -nt file2?
True if file1 is newer than file2.

`file1 -ot file2?
True if file1 is older than file2.
String tests
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?
True if the length of String is zero.

`-n String?
`String?
True if the length of String is nonzero.

`String1 = String2?
True if the strings are equal.

`String1 != String2?
True if the strings are not equal.
Numeric tests
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?
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
= yes
test -l abc -gt 1 && echo yes
= yes
test 0x100 -eq 1
error-- test: integer expression expected before -eq
Examples
$ ls -altotal 67

drwxr-xr-x 18 root root 4096 Jun 9 21:12 ./drwxr-xr-x 18 root root 4096 Jun 9 21:12 ../-rw-rw-rw- 1 simon users 4096 Jun 9 07:30 london-rwsrwsrwx 1 simon users 4096 Jun 9 07:32 aberdeen-rw------- 1 simon users 4096 Jun 9 07:29 bristol-rw-r--r-- 1 simon users 4096 Jun 9 07:29 bath$
$ test -r paris
$ echo $?
1
$
Because the file `paris? is not Readable, the value returned
is false (non zero)

if [ "$LOGNAME" = "scott" ]then echo "Logged in as Scott"else echo "incorrect user"fi
If the logname variable = scott then the test returns TRUE (0)
"The test of a vocation is the love of the drudgery it involves" - Logan Pearsall
Tags
Comments
Write the first comment
Leave a trace
Name *
Email *
Website
Anti SPAM * Code (3 + 9) =
Leave me a comment *
 
All comments are subject to editorial review
Post being viewed right now
Item date: 13.05.2009
Views: 876
Item date: 24.08.2009
Views: 347
Item date: 17.06.2009
Views: 1769
Item date: 04.02.2009
Views: 800
Item date: 09.03.2009
Views: 885
Item date: 01.01.2009
Views: 941
Item date: 04.02.2009
Views: 1735
Item date: 24.08.2009
Views: 296
Item date: 03.01.2009
Views: 817