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

I'm with you, the [ (and [[ bashism) introduces lots of confusions about what is really happening, I could never manage any real confidence.

That said, [[ being guaranteed to be built-in certainly had its purpose at ages where shell script performance had any kind of relevance, and that was no so long ago.



[[ has more to do with trying to build an intuitive shell scripting environment than performance. [[ makes conditionals behave much more like you'd expect from other programming languages. I think it's a great idea, but then again, if I don't have to care for POSIX portability, I'd rather use something that's not a shell language for scripting.


While they were at it, just to be consistent, they should have added syntax for easy-to-use non-fucked-up less-arbitrarily-punctuated versions of control structures, too:

    ifif [[ x == 1 ]] thenthen
      echo "x is one"
    elseelse
      echo "x is not one"
    fifi

    forfor x in 1 2 3 dodo
        echo "x is $x"
    donedone

    whilewhile [[ x == 1 ]] dodo
      echo "x is one"
      x=$((x + 1))
    donedone

    untiluntil [[ x != 1 ]] dodo
      echo "x is not one"
    donedone
The whole [[ ]] $(( )) ifif fifi dodo thing just seems like they're just doubling down instead of admitting they made a mistake.

And if they really wanted [[ ]] to seem like syntax instead of a shell command, they could at least allow it to be used without spaces on each side of it like $(( )) or parens or brackets in any other language. And every other language lets you use as many redundant parens as you like for clarity, without running twice as many commands or producing a syntax error or weird unexpected behavior. But I don't think clarity was ever a design goal with Unix shell scripting languages.


> arbitrarily-punctuated versions of control structures

Ahem:

  if [ x = 1 ]
  then echo "x is one"
  else echo "x is not one"
       echo "namely, it's $x"
  fi
`if [ ] ; then` should only ever be used in one-liners, where there is not a newline after `then`; I'm not sure how that ended up being taught as a way to write multi-line commands (I'd tenatively blame Pascal, but that's probably unfair).




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

Search: