I have seen scripts using the test
command and [ ]
or [[ ]]
. But when do we need to use /usr/bin/test
and (( ))
?
Are there any occasions when we need to go for the latter?
Regards, John
/usr/bin/testThe test utility evaluates the condition and indicates the result of the evaluation by its exit status.
In bash shell, the test command compares one element against another and returns true or false. In bash scripting, the test command is an integral part of the conditional statements that control logic and program flow.
A test command is a command that is used to test the validity of a command. It checks whether the command/expression is true or false. It is used to check the type of file and the permissions related to a file.
To answer your question:
/usr/bin/test
when you want to test
something but not in a shell (for example find ... -exec test ...
)(( ))
when you have an arithmetic expression to solve, AND you are using bash, because (( ))
is bash specific.Now for some background:
The command /usr/bin/test
is required by the POSIX standard. POSIX also requires that [
is defined as an alias for test
. The only difference between test
and [
is that [
requires the final parameter to be a ]
.
Since test
is used so frequently in shell scripts, most shells have a builtin version of test
(and [
). The advantage of a builtin is that it avoids context switches. Which, depending how you use test
in your script, can be a measurable performance advantage.
I think it is safe to assume that under most circumstances it doesn't matter whether you use the system test
or the shell's builtin test
. But if you want to use test
in a find -exec
situation then of course you have to use the system test
because find
cannot use the shell test
.
(( ))
and [[ ]]
were introduced by bash (and perhaps some other shells) as syntactic sugar. (( ))
evaluates arithmetic expressions, while [[ ]]
evaluates logical expressions. Both allow you to write the expressions in a "more natural syntax".
The decision to use [[
or [
depends on whether you want to use the "more natural syntax", and, since sh does not support [[
, whether you want to depend on bash.
The decision to use (( ))
depends on whether you need arithmetic expressions, and again, since sh does not support (( ))
, whether you want to depend on bash. The POSIX alternative to (( ))
is $(( ))
. Note that there are some subtle differences in the behaviour.
The following links explain these topics in great detail:
test
, [
and [[
)let
, (( ))
and $(( ))
)See also:
test
$(( ))
Bonus: Some debian developers once argued whether they should use the system test
or the shell builtin test
, because of some differences in the implementation of the builtin test
. If you are interested in details of the differences of the system test
and the shell builtin test
then you can read the debian developer discussion here: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=267142.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With