Re: bash if statement

Posted 13 July 2008 - 08:24 AM

Well, I think the reasoning behind the syntax is easy once one understands the underlying mechanisms here. You see, the [ and ] are not part of the shell language (sometimes they are, but they have to be compatible with older shells). The [ in the if compound statement is really either /bin/[ (a symlink to /bin/test), or is a built in. So, "if [ -z $X ] ; then" should be similar to "if test -z $x ; then". The test binary will ignore the "]" purposely, just to support this functionality. The if statement simply executes whatever comes after it, and evaluates the exit code in order to decide what block to execute thereafter.

BTW, some shells offer a [[ builtin as well, that usually supports a superset of what can be given to the [ external/builtin.