Live data from Hacker News

Test your square brackets

fluca1978.github.io

31–40 of 68 posts

Re: Test your square brackets

#32

Earlier quoted context omitted.

It doesn’t have anything to do with bash (though modern bash may use a built in for `[`). He don’t have the `[` program (usually linked to `test`).

Which is why later versions of bash have a builtin… Precisely because those older systems didn’t link to it! So my comment still stands.

[deleted]

Re: Test your square brackets

#33

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)

Why do you think the prof even used bash? I highly doubt his ancient SunOS machine had a GNU toolchain. `test` and `[` are both POSIX, but if there was no `/bin/[` I doubt the shell in question (original Bourne? Some proprietary Sun shell? Who knows) had it built in either.

Re: Test your square brackets

#34

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.

If shipping something that must run on sh, check your life choices and use [ - otherwise [[ is better.

Honestly though I’ve been much happier since I stopped writing anything complex enough to have conditionals in Shell. Using a real scripting language like Ruby, Python, even PHP or Perl if you know them, is better.

In the Ruby case I just use `%x( … )` when I need to run shell commands (there are some things shell does great like passing pipelines of text through 5 programs) and let the logic part be in Ruby.

Re: Test your square brackets

#35
post #5

Earlier quoted context omitted.

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

Yeah, if you set the shebang `#!/bin/sh` (which is portable), shellcheck will complain about double brackets. It also helps you do quoting correctly when using single brackets.

Re: Test your square brackets

#37
post #29

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.

yeah, if [ is a command in /bin/ what is [[ ?

A builtin part of the shell, just like `if` and `then` and `fi`. Not all shells have `[[`, the one that doesn't which people unknowingly run into most is probably `dash` as used by Alpine Linux. POSIX doesn't require `[[` in a shell, BASH & Zsh support it but others often don't.

Re: Test your square brackets

#38
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

Right, that's also a good reminder that the builtin `command -V` is typically what you want whenever you use not-always-builtin `which` ;)

Re: Test your square brackets

#39

Earlier quoted context omitted.

Use ((...)) for arithmetic tests and [[...]] for other tests. [...] is for POSIX compatibility and not as useful as [[...]] though I don't remember the specifics.

[[…]] is a bash (probably other shells, too) built in. […] could be a built in, or could be a symlink to /bin/test.

The [[…]] version was introduced in Peter Korn's korn shell, ksh88 IIRC.

Re: Test your square brackets

#40
post #21

Reminds me of my rather memorable introduction to special characters invoking functions, seeing this dastardly little quip in the email signature of someone in a mailing list (circa '95 or so). :(){:|:&};: My curiosity piqued, I pasted it into the shell on my terminal in a pure example of FAFO. The poor little Sparc 5 I was using ground to a halt over the course of about ten seconds. The reboot was as hard as the les…

> The reboot was as hard as the lesson.

I'm confused. What happened on reboot?

Post reply on HN