Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use (( )) or /usr/bin/test

Tags:

shell

unix

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

like image 518
John Avatar asked Jun 30 '11 12:06

John


People also ask

What is usr bin test?

/usr/bin/testThe test utility evaluates the condition and indicates the result of the evaluation by its exit status.

What does test do in bash?

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.

What is test in shell script?

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.


1 Answers

To answer your question:

  • you want to use /usr/bin/test when you want to test something but not in a shell (for example find ... -exec test ...)
  • you want to use (( )) 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:

  • http://mywiki.wooledge.org/BashFAQ/031 (difference between test, [ and [[)
  • http://mywiki.wooledge.org/ArithmeticExpression (let, (( )) and $(( )))
  • http://www.ibm.com/developerworks/library/l-bash-test/index.html (all of the above)

See also:

  • POSIX definition of test
  • POSIX definition of $(( ))

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.

like image 145
Lesmana Avatar answered Sep 18 '22 14:09

Lesmana