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?
[[ 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[[ $foo = $bar ]]
and[[ $foo = "$bar" ]]
are not equivalent; forfoo=abc; bar=a*c
the one will be true, the other false. – Sep 09 '19 at 23:50