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

Thanks hn for doing what you do! I'm so glad to see this here.

A few months back I noticed '[' under /bin on my mac. I tried googling to understand what it was, but my google-fu came up short. It felt like one of those ungoogleable things. This link is an excellent starting point for me.



Searching for /bin/[ gets reasonable results for me

https://www.google.com/search?q=%2Fbin%2F%5B


Doesn't mac have manual pages for shell utilities? In other words, this should work: man [


If I can go "man [", then why doesn't "man (" work? And why aren't there man entries for the rest of all the punctuation marks that bash uses?


They look similar, but to your shell they are different: [ is the name of an executable, ( is a syntax symbol of bash. The man page for the syntax is the man page of bash.


it is funny that [ is the name of binary but when using it, you can't just type [ without the closing ]

by the name, you'd think you could just use [ 0 -gt 1 <enter>

after all, we don't have to type grep query perg


What's not so funny is that /bin/[ has no way of enforcing this syntax rule, or any other. Just like sed can't enforce any syntax rules on its regular expressions. This is why the shell is still a crock of shit for scripting even with "set -e" on.

The invoked binary has no way of aborting script execution. All it can do is barf out on stderr and return an error code, which the shell interprets as false and `if [ "x" = "x"` (without ]) goes into the else branch.


I like the cut of your jib. Finally a breath of fresh air from a sane person amongst all the crazy people making fanatically apologetic excuses and heaping evangelical praise on toxic burning dumpster fires.


That does work if you call it as `test`; just a quirk of faking shell syntax in an external binary I think.


I was halfway expecting /bin/[ -> /bin/test, but that did pass the smell test since the ] is required, so it couldn't just be symlink


It could have still been implemented like that in the filesystem, if the binary read the name it was executed under and modified its behavior based on that. As an extreme case, on a stock Alpine Linux system, both test and [ - along with most other core system programs - are all symlinks to a single busybox binary that reads argv[0] and acts like whatever program it's been called as. I'm actually somewhat surprised that GNU didn't do that in this case; I, too, would have expected test and [ to be some manner of link to the same program, either with identical behavior or using invocation name to decide how to behave.


There's the old $0 trick. I use that for shell scripts that share a lot of code.

    if [[ $0 == "norg" ]]; then
      gron --ungron "$@"
   
And so on


Are norg and ungron similar to wobble and wibble?


The really funny part is running it as /bin/[ probably requires omitting the ].


nope, but that would be as easy as trying it in the terminal to see that your guess is not correct at all




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

Search: