Hi
what is the difference between following:
if [ condition ]
then
//some code
fi
AND
if test [ condition ]
then
//some code
fi
Hi
what is the difference between following:
if [ condition ]
then
//some code
fi
AND
if test [ condition ]
then
//some code
fi
if ... requires a program to run, it then tests the exit code of the process to determine which branch to take.
test is a program which returns an exit code based of various expressions given as arguments.
[ actually a program look for /bin/[ or /usr/bin/[ in the file system, this is often a link to the program "test".
So "if test [" is actually like saying "if test test" or even "if /bin/test /bin/test".
The following is from Mac OSX 10.4.9...
$ ls -l /bin/test /bin/[
-r-xr-xr-x 2 root wheel 18104 Mar 21 2005 /bin/[
-r-xr-xr-x 2 root wheel 18104 Mar 21 2005 /bin/test
The second one is invalid.
Valid syntax :
if test condition
then
//some code
fi
[ -z this ]
is just shorthand for
test -z this
they are exactly equivalent
man test