Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

put two conditions in if

 
SOLVED
Go to solution
Highlighted
Dennis Handly
Acclaimed Contributor

Re: put two conditional expressions in if

>the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external test(1) command

 

This is incorrect.  test is documented as a builtin and tusc shows that ksh has [ ... ] builtin.

And especially since /usr/bin/test is just a Posix shell script that invokes the sh test builtin.

Stephan.
Honored Contributor

Re: put two conditions in if


@Dennis Handly wrote:

>the second form is more efficient because the '[[ ]]' is evaluated directly by the shell (Korn or POSIX) whereas the more traditional '[ ]' is evaluated by the external test(1) command

 

This is incorrect.  tusc shows that ksh has [ ... ] builtin.

And especially since /usr/bin/test is just a Posix shell script that invokes the builtin.


So to give it some sense to dig up this one - what would be the technical exact answer for the difference of

 

[ ]

or

[[ ]]

 ?

Dennis Handly
Acclaimed Contributor

Re: put two conditional expressions in if

>what would be the technical exact answer for the difference of: [ ... ]  [[ ... ]]

 

There are three ways to compare in a real shell:

 1) Arithmetic expressions: (( )), only handles numbers, not strings

 

2) [ ... ] or test builtin

    Compound expressions are composed with -a and -o.  Use of () must be quoted.

    Arithmetic expressions, without (( )), can be used in arithmetic comparison operators.

    String = and != compare strings.

    String < and > don't exist.  (Treated as redirection.)

 

 3) [[ ... ]]

    Compound expressions are composed with && and ||.  Use of () doesn't need to be quoted.

    Arithmetic expressions can't be used in arithmetic comparison operators.

    String = and != match patterns.

    String < and > exist.