Test your square brackets
31–40 of 68 posts
Re: Test your square brackets
#32Earlier 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.
Re: Test your square brackets
#33The 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
#34Now 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.
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
#35Earlier 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
Re: Test your square brackets
#36The real solution is that as soon as you need square brackets, switch to a better language than bash
Re: Test your square brackets
#37Now 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 [[ ?
Re: Test your square brackets
#38Nowadays [ 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
Re: Test your square brackets
#39Earlier 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.
Re: Test your square brackets
#40Reminds 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…
I'm confused. What happened on reboot?