Remix.run Logo
theandrewbailey 6 hours ago

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.

PhilipRoman 6 hours ago | parent | next [-]

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

account42 5 hours ago | parent | next [-]

It also does wildcards though, with POSIX you'll need a case statement for that.

ndsipa_pomu 5 hours ago | parent | prev [-]

I'm intrigued - any info on that?

I personally use ((...)) for arithmetic tests and [[...]] for all other tests as I just target new versions of BASH and don't care much about POSIX compatibility.

xp84 4 hours ago | parent | prev | next [-]

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.

nickjj 6 hours ago | parent | prev | next [-]

[[ ... ]] supports regex comparisons and lets you combine multiple conditions in a single bracket group using && and || instead of remembering to use -a / -o.

I usually default to [ ... ] unless I need features that double brackets provide.

stabbles 6 hours ago | parent | prev | next [-]

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

When unsure, use shellcheck.

duskdozer 6 hours ago | parent | next [-]

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

stabbles 4 hours ago | parent [-]

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.

a3w 6 hours ago | parent | prev [-]

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

ndsipa_pomu 5 hours ago | parent | prev | next [-]

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

jonhohle 5 hours ago | parent [-]

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

jcynix 4 hours ago | parent [-]

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

zzzeek 4 hours ago | parent | prev [-]

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

SAI_Peregrinus 4 hours ago | parent [-]

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.

zzzeek 4 hours ago | parent [-]

right so "[" is a file in /bin, "[[" is implicitly part of the shell, so that's as bad as I thought