uebv / google-styleguide

Automatically exported from code.google.com/p/google-styleguide
0 stars 0 forks source link

shell (bash) non-empty string test works without -n #20

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The following warning is wrong:
https://code.google.com/p/google-styleguide/source/browse/trunk/shell.xml?r=112#
716

[[ "${my_var}" ]] is, when quoted, always exactly the same as [[ -n "${my_var}" 
]] regardless of my_var contents, as far I've tested. Even with [ builtin. Can 
you provide a counterexample?

Original issue reported on code.google.com by kernc...@gmail.com on 9 Nov 2013 at 3:37

GoogleCodeExporter commented 9 years ago
Indeed, the comment is likely incorrect. For the standard single-bracket test 
("["), POSIX.1-2008 specifies the following behavior, which precludes a single 
argument being interpreted as a test flag:

    In the following list, $1, $2, $3, and $4 represent the arguments presented to test:
    ...
    1 argument:
        Exit true (0) if $1 is not null; otherwise, exit false.

bash's "[[" appears to behave the same way (even when ${my_var} is not quoted, 
as field splitting and pathname expansion are not performed inside [[ ]]).

Original comment by nate.we...@gmail.com on 15 May 2014 at 3:47