| ▲ | 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? | |||||||||||||||||||||||
| |||||||||||||||||||||||
| ▲ | 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. | |||||||||||||||||||||||
| |||||||||||||||||||||||
| ▲ | 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. | |||||||||||||||||||||||
| |||||||||||||||||||||||
| ▲ | zzzeek 4 hours ago | parent | prev [-] | ||||||||||||||||||||||
yeah, if [ is a command in /bin/ what is [[ ? | |||||||||||||||||||||||
| |||||||||||||||||||||||