3 ms·
Isn't this one of the things that bash [[ ]] explicitly fixes?
by misnome 4y ago
Isn't this one of the things that bash [[ ]] explicitly fixes?
- hgsgm 4y agoI hope not, because $empty is necessary when you actually want to elide a missing value, like Python's `filter`
- rullelito 4y agoWhat a horrible language:x
- digitalsushi 4y agoIt's horrible but it's home for a lot of us.
- Already__Taken 4y agomore like holm for you.
- Spivak 4y agoThe parent I think is getting two things mixed up. [[ $var == "" ]] works as you describe and as expected. What the parent is talking about is when you have an invocation like my_program $opt my_program "$opt" Which when empty will evaluate to my_program my_program "" respectively.
- naniwaduni 4y agoWell, on the left side. On the right side, [[ still requires you to quote, otherwise: % bash -c 'a=xy; b="x*"; [[ $a == $b ]] && echo "$a == $b"' xy == x* My hot take is that you should avoid using [[, preferring to use [, which at least pretends to work like a normal program that can be invoked using normal quoting rules—it just interprets a funky mini-language on its command-line arguments. [[ is some exotic thing that seems reasonable at first glance but follows no rules but its own and varies its behavior depending on syntactic nits that literally cannot possibly matter and aren't even distinguishable for normal programs. And then just quote consistently, which is something you need know how to do with almost every other line of a shell script anyway!
- cellularmitosis 4y ago> you should avoid using [[, preferring to use [ I tend to agree, and have taken this one step further, preferring the "test" command: if test "$a" = "foo" ; then I do this for a few reasons: - "Subsetting" your language can be beneficial: https://wiki.c2.com/?LanguageSubset https://wiki.c2.com/?LanguageSubset - "test" reinforces the notion that this is just the name of a program, not a syntax construct, which encourages using other programs as predicates, e.g. 'if which -s foo'. - "man test" doesn't force you to wade through a 5,000 line man page. Recent example: https://leopard.sh/leopard.sh https://leopard.sh/leopard.sh
- lanstin 4y agoWow haven’t seen a c2 link not from me in so long. So much good and interesting thought there.
- yrro 4y agoWell techincally... the == and != operator matches the LHS against the pattern on the RHS. So [[ $a == "$b" ]] works. $ shellcheck oops.sh In oops.sh line 4: [[ $a == $b ]] ^-- SC2053 (warning): Quote the right-hand side of == in [[ ]] to prevent glob matching. For more information: https://www.shellcheck.net/wiki/SC2053 -- Quote the right-hand side of == i... Unlike the bugs that the x$var works around this is all in the manual! https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b https://www.gnu.org/software/bash/manual/bash.html#index-_00...