0

I'm using shfmt (a tool to tidy up bash scripts). It has a -s (simplify) option that rewrites a line like:

if [[ -n "$out" ]]

to unquoted version:

if [[ -n $out ]]

I'm actually curious if unquoting variables is safe for string-based comparisons/tests in [ and [[. In other words, is the tool rewriting the code to be any better?

I feel like there's no harm in the quoted version, it would definitely prevent splitting but I guess [ and [[ aren't really susceptible to splitting?

ahmet alp balkan
  • 601
  • 1
  • 8
  • 21
  • 1
    Related: [When is double-quoting necessary?](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary) – steeldriver Sep 09 '19 at 23:41
  • 1
    Yes, it is safe. Quoting from the holy scripture: "`[[ expression ]]` ... Word splitting and pathname expansion are not performed on the words between the `[[` and `]]`". I don't know about that tool or whether other simplications it does are safe. –  Sep 09 '19 at 23:46
  • 2
    Especially, `[[ $foo = $bar ]]` and `[[ $foo = "$bar" ]]` are not equivalent; for `foo=abc; bar=a*c` the one will be true, the other false. –  Sep 09 '19 at 23:50
  • 1
    Also: https://unix.stackexchange.com/q/306111/70524 – muru Sep 09 '19 at 23:57

0 Answers0