Can these two variable-substitution-syntaxes combined into one?

rm "$s_a/$domain.conf" 2>/dev/null
rm "$s_e/$domain.conf" 2>/dev/null


rm "$s_{a,e}/$domain.conf" 2>/dev/null
  • I did a mistake not to test this. I also remember I got working results in a similar case with ${} syntax. My bad. – JohnDoea Nov 7 '18 at 1:58

You can, but not while quoting the expansion (since double-quotes preserve the literal meaning of most "special characters", braces included):

rm $s_{a,e}/$domain.conf

expands in steps to:

rm $s_a/$domain.conf $s_e/$domain.conf

and then to the corresponding values.

Within double-quotes, the braces are left alone, so the line expands to:

rm (value of $s_){a,e}/(value of $domain).conf

Quoting prevents unintentional expansions, notably word splitting and filename expansion. You could work around word splitting by unsetting $IFS, and could work around filename expansion with set -f, combining to:

set -f
rm $s_{a,e}/$domain.conf 2>/dev/null
set +f
  • I can't say why other people voted as they did, just that you could help yourself by demonstrating some testing on your part and point out where you were surprised or confused. – Jeff Schaller Nov 6 '18 at 21:24
  • I fully agree this should generally be done. – JohnDoea Nov 6 '18 at 21:52
  • 2
    To not have to bother about whether set -f was in effect previously, or waht IFS was set to: ( IFS=; set -f; rm ... ) – Kusalananda Nov 6 '18 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.