Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Definitely quote. `[ x$X == x"hello" ]` may fail for a variety of different values of X like `*`, `Rocky_[1976].jpg`, `hello world` and `foo = xfoo -o bar` Quoting works in all those cases.


Of the listed, it should only actually fail on the ones with spaces in them (or rather, the ones that get split according to IFS).


Whether or not it should, it may fail for any kind of glob.

This could be either because the glob ends up unintentionally matching something:

    $ var='*'; touch xfoo xbar
    $ [ x$var == x"hello" ]
    bash: [: too many arguments

    $ var="Rocky_[1976].jpg"; touch xRocky_7.jpg
    $ [ x$var == x"Rocky_[1976].jpg" ]
    (false, should be true)
or because you have something like nullglob or failglob enabled:

    $ var="Rocky_[1976].jpg"; shopt -s nullglob
    $ [ x$var == x"hello" ]
    bash: [: ==: unary operator expected




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: