[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: /bin/sh diversions

Oleg Verych dixit:

>02-08-2007, Peter Samuelson:

>>   unset foo
>>   [ -n $foo ] && echo foo is non-empty
>>   [[ -n $foo ]] && echo foo is non-empty
>> As you can see, only the second one works.

True, that's why the Korn shell invented [[.

>Not quoting possible empty argument is a script writing bug.

This is actually wrong; in some cases, additional quoting is
a bug (think of “backticks”, which you shouldn't use anyway,
but I don't have a better example right now, except for the
operators to [[ which must be unquoted) or at least undesi-
rable (the argument to “case”, for example), and [[ was spe-
cifically designed to NOT expand its arguments as if they
were arguments to an external programme.

     [[ expression ]]
             Similar to the test and [ ... ] commands (described later), with
             the following exceptions:

                 + o Field splitting and file name generation are not per-
                       formed on arguments.

                 + o The -a (AND) and -o (OR) operators are replaced with
                       '&&' and '||', respectively.

                 + o Operators (e.g. '-f', '=', '!') must be unquoted.

                 + o The second operand of the '!=' and '=' expressions are
                       patterns (e.g. the comparison [[ foobar = f*r ]]

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

                 + o Parameter, command, and arithmetic substitutions are
                       performed as expressions are evaluated and lazy expres-
                       sion evaluation is used for the '&&' and '||' opera-
                       tors. This means that in the following statement,
                       $(<foo) is evaluated if and only if the file foo exists
                       and is readable:

                             $ [[ -r foo && $(<foo) = b*r ]]

I believe no one can invent an algorithm. One just happens to hit upon it
when God enlightens him. Or only God invents algorithms, we merely copy them.
If you don't believe in God, just consider God as Nature if you won't deny
existence.		-- Coywolf Qi Hunt

Reply to: