test (Unix)
test ( /bin/test
) is a Unix tool that can be used to make logical comparisons. It is part of the basic equipment of Unix-like systems and its behavior is standardized by the POSIX standard.
Many shells now implement it as a built-in command, but these implementations are all derived from the original version as a stand-alone program.
For usability reasons, the program also exists under the name /bin/[
, which /bin/test
behaves exactly like , only that it ]
expects as the last argument . See story .
Working method
By convention, Unix programs interpret (return) values of 0
as logical wahr
, values unequal 0
as unwahr
or falsch
. test
parses the simple or compound logical expression described in the transferred arguments and shows the result as a return code ( RC ) 0 or 1. Since the branch operations of the shell scripting languages typically depend on this return value, a generalized method of parameterizing branches is created.
In the following application do..while
example , the loop is run through as long as the value is Counter
less than or equal to 100:
#! /bin/ksh
typeset Counter=0
while test $Counter -le 100 ; do
(( Counter += 1 ))
done
parameter
/bin/test
accepts as arguments on the one hand values ( strings , integers and file names), on the other hand logical operators (comparisons, conditions of existence of file metadata and logical links). From this, the truth value of a simple or compound logical function is determined. The operators are divided into:
File-related operators
File operators take a single path name as an argument. The existence of a file ( -e <file>
: the file exists ), the existence of certain access rights ( -r <file>
: the file exists and is readable ) or a certain file type ( -d <file>
: the file exists and is a directory ) can be checked.
Integer operators
Integer operators expect two integer values as arguments, which are compared according to the operator ( <Int1> -ge <Int2>
: Int1 is greater than or equal to Int2 ). This corresponds to the comparison operators >
, >=
, ==
etc. of most programming languages. Since the shells usually do not differentiate between data types, the calling program (usually a script) must ensure that the values passed can also be interpreted as integers. Arguments that do not match this will result in a runtime error.
String operators
String operators expect two strings as arguments and check for equality or inequality ( "<String1>" = "<String2>"
= String1 is identical to String2 ). Because of the lack of type accuracy of the shell languages mentioned above, integer values as strings cannot be compared with one another in reverse. The ambiguity of comparison operators that is familiar from other programming languages, i.e. that for example determines ==
the equality of strings and numerical values, is expressly not given.
Logical operators
Several of the above-mentioned individual conditions can also be linked by logical UND
or ODER
with each other ( <Int1> -ne <Int2> -a -r <file>
: <Int1> is not the same as <Int2> AND the executing process has read rights to <file> ). However, the POSIX standard restricts the behavior of test
"unspecified" when more than 4 arguments are passed.
Operator precedence, grouping
The precedence of logical operators, which is also common in other programming languages, implicitly applies, but behavior that deviates from this can also be \(...\)
enforced with the help of grouping symbols ( ). Due to the limitations of the POSIX standard, this has hardly any meaning, but the vast majority of real implementations can process significantly more (and an unlimited number for practical purposes) of arguments than the specified 4:
#! /bin/ksh
typeset -i x=0
typeset -i y=1
typeset z=""
if test \( $x -gt 0 -a $y -ne 0 \) -o "$z" != "" ; then
print - "( falsch UND wahr ) ODER wahr -> wahr"
fi
if test $x -gt 0 -a \( $y -ne 0 -o "$z" != "" \) ; then
:
else
print - "falsch UND ( wahr ODER falsch ) -> falsch"
fi
history
The original design was test
as it exists today. However, since the appearance of shell programs differed significantly from that of other programming languages, test
a link was also [
made available as. That changed the appearance of scripts to:
#! /bin/ksh
typeset -i Counter=0
while [ $Counter -le 100 ; do
(( Counter += 1 ))
done
but would have led to the somewhat unusual circumstance that apparently an open bracket is not closed. So the binary was changed ]
to expect a final argument when it is [
called as. This defined the appearance in script languages that is common today. This is also the reason why [
and ]
must always be surrounded by spaces (the shell's internal field separator ):
#! /bin/ksh
typeset -i Counter=0
while [ $Counter -le 100 ] ; do
(( Counter += 1 ))
done
Many current operating system versions replace the original link from /bin/test
to /bin/[
with their own binary, which, however, has no effect on the function.
test
as a shell built-in
Most modern shells provide a similarly functioning built-in command for performance reasons . The Kornshell and Bourne-again shell implement this test
as [[
the logical ]]
expectation as the final argument. However (in contrast to the conventional one test
) neither filename expansion nor field splitting is performed. This influences, for example, checks on file names that contain spaces, as the example illustrates: if there is a subdirectory with a name in the working directorytest dir
, then the first test will be positive (because the field splitting did not take place , "test dir" is used as an identifier despite the omitted quoting used), while the second will fail after field splitting has been carried out because the search is only for "test".
#! /bin/ksh
typeset FOO="test dir"
if [[-d $FOO]] ; then
print - "internal test: dir $FOO found"
fi
if [ -d $FOO ] ; then
print - "regular test: dir $FOO found"
fi
Web links
-
test
: evaluate expression - Open Group Base Specification -
test(1)
: condition evaluation utility - OpenBSD General Commands Manual -
test(1)
: Check file types and compare values - Debian GNU / Linux Executable programs or shell commands man page
Individual evidence
- ↑ a b test specification of the Open Group. Retrieved April 13, 2013 .