Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GoogleCodeExporter opened this issue May 20, 2015 · 3 comments
Closed

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

GoogleCodeExporter opened this issue May 20, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

m-chaturvedi pushed a commit to m-chaturvedi/styleguide that referenced this issue Feb 4, 2020
@vapier
Copy link
Member

vapier commented Feb 17, 2020

this is likely obsolete. the current shell style guide tells people to use explicit -z and -n for clarity purposes. which is what we want. implicit empty string tests are bad form.

@IsaacG
Copy link
Contributor

IsaacG commented Feb 17, 2020

To avoid confusion about what you're testing for, explicitly use -z or -n.

@IsaacG IsaacG closed this as completed Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants