Bash: Brace expansion in scripts not working due to unwanted escaping

Viewed 6819

I want to do something like this in a bash script. I'm using bash 4.1.10.

# rm -rf /some/path/{folder1,folder2,folder3}

Works nicely (and as expected) from the shell itself. It deletes the 3 desired folders leaving all others untouched.

When I put it into script something unwanted happens. For example, my script:

#!/bin/bash
set -x
VAR="folder1,folder2,folder3"
rm -rf /some/path/{$VAR}

When I execute this script, the folders are not deleted.

I think this is due to the fact that some unwanted quoting is occurring. Output from the script using #!/bin/bash -x:

rm -rf '/some/path/{folder1,folder2,folder3}'

which of course cannot succeed due to the ' marks.

How can I get this working within my script?

9 Answers
Related