I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.
I'm still not sure when to use one or the other. I use double brackets by default until something doesn't work.
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.
This can evaluate arbitrary code: [[ "${payload}" -eq 42 ]]
Here is one example of a malicious payload:
payload='a[$(touch /tmp/pwned)]'Now I need to figure out whether (( payload == 42 )) is safe.
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.
I usually default to [ ... ] unless I need features that double brackets provide.
When unsure, use shellcheck.
And this isn't meant snarkily - why should I care if my scripts are portable? I've worked on a number of different devices and types but have seemingly always had access to bash.