Live data from Hacker News

Test your square brackets

fluca1978.github.io

1–10 of 68 posts

Re: Test your square brackets

#3
The ultimately sad part was the professor in a Sun OS machine.

In a corner with no where to go, giving demerits because his bash was older than he realized.

Reminds me of my college professor that claimed you don’t have to close HTML tags (some you absolutely do) and I proved that you do. Not all of them, but most of them. (Netscape Navigator Days)

Re: Test your square brackets

#4
> When I was a young, green, university student, I was forced to use test(1) as the only true way to do testing in shell scripting. […] Yeah, I was also forced to not use semicolons as they were evil (according to my professor, any comment unneeded!).

The author’s professor clearly went overboard, but doesn’t this entire anecdote demonstrate the value of teaching it this way? Having green students call the `test` binary provides more insight into how UNIX operates, and gets them using a UNIX mindset. They can discover the syntactic shortcuts later.

Re: Test your square brackets

#5

Now do [ ... ] and [[ ... ]] I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.

Double brackets are less portable. For example musl linux does not come with bash by default, and your script fails.

When unsure, use shellcheck.

Re: Test your square brackets

#7
post #5

Now do [ ... ] and [[ ... ]] I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.

Double brackets are less portable. For example musl linux does not come with bash by default, and your script fails. When unsure, use shellcheck.

[[ is built in, so "test[" as an /usr/bin artifact never exists? (What to call that proposed program, test2, or test[ ?)

Re: Test your square brackets

#8

Now do [ ... ] and [[ ... ]] I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.

[[...]] is non-portable and has an extremely quirky corner case with variable expansion in arithmetic contexts, what's not to love?

Re: Test your square brackets

#9
post #5

Now do [ ... ] and [[ ... ]] I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.

Double brackets are less portable. For example musl linux does not come with bash by default, and your script fails. When unsure, use shellcheck.

You mean shellcheck will detect when single brackets won't be enough? I've also just defaulted to double because I never really looked into it

Re: Test your square brackets

#10
post #6

Nowadays [ is a builtin. The subprocess for a simple branch would be excessive overhead.

It is indeed a builtin, but `/bin/[` still exists for compatibility reasons!

   $ which [
   /bin/[
   $ type [
   [ is a shell builtin
The same is true for the `test` command:

   $ which test
   /bin/test
   $ type test
   test is a shell builtin
Post reply on HN