Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kshell test function 2

Status
Not open for further replies.

lazyrunner50

Programmer
Jul 30, 2004
63
US
Ok, so this is a simple question, but I can't find any explanation on google. Anyway, what is the difference between [ and [[ for test conditions? For instance:

Code:
if [[ -z $myVar ]] && [[ -z $myVar2 ]]; then
do something
fi
and
Code:
if [ -z $myVar ] && [ -z $myVar2 ]; then
do something
fi
 
man ksh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
My understanding is that the different conditional expression operators exist mainly for historical reasons now. [ used to be another name for /usr/bin/test, and follows the same semantics (see man test) whereas [[ was implemented by more advanced shells than the Bourne shell (/usr/bin/sh) such as Korn and BASH as a shell keyword. It seems like both of them are implemented as shell keywords/builtins these days.

On some operating systems they accept different syntax, i.e. [ uses -a and -o for logical AND and OR operations, whereas [[ uses && and ||, but I notice that on HP-UX the [ form seems to accept both syntax. [[ however only accepts the newer && style.

Annihilannic.
 
Thanks Annihilannic, I did some experimenting with it (on Solaris), and saw that some conditions would work with [[ whereas they wouldn't with [. I think you're right about using the double one with && and || operators though because I've seen some cases where the conditions fail if I use the single [.

PHV, thanks, but I already looked at that! :-( Man pages, while very useful in a bind, aren't good at explaining sometimes (man page for man pages anyone??)
 
I found this on the web and hope it benefits you.

[[ expression ]]

Similar to the test and [ ... ] commands , with the following exceptions:


· Field splitting and file name generation are not performed on arguments.

· The -a (and) and -o (or) operators are replaced with &&
and ||, respectively.

· Operators (e.g., -f, =, !, etc.) must be unquoted.

· The second operand of != and = expressions are patterns
(e.g., the comparison in [[ foobar = f*r ]] succeeds).

· There are two additional binary operators: < and > which
return true if their first string operand is less than,
or greater than, their second string operand, espectively.

· The single argument form of test, which tests if the
argument has non-zero length, is not valid - explicit
operators must be always be used,
e.g., instead of [ str ] use [[ -n str ]]

· Parameter, command and arithmetic substitutions are performed
as expressions are evaluated and lazy expression
evaluation is used for the && and || operators. This
means that in the statement [[ -r foo && $(< foo) = b*r ]]
the $(< foo) is evaluated if and only if the file foo
exists and is readable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top