Search code examples
bashquoting

How does bash quoting work with curly brace {} expansion?


I'm trying to write a bash script to reproduce a failing test.

The original command was (say we are testing echo)

echo aa @p{0,1}=port bb

which prints:

aa @p0=port @p1=port bb

I've got the arguments in a variable

TESTCASE='aa @p{0,1}=port bb'

so I build the command line

CMD='echo '${TESTCASE}

and then execute it

${CMD}

and I get:

aa @p{0,1}=port bb

i.e. the curly braces don't expand like they would on the command line

What is going on and how do I fix it?


Solution

  • from comments by Aserre and glenn jackman, combined:

    Brace expansions happen before variable substitution: see gnu.org/software/bash/manual/bashref.html#Shell-Expansions

    eval $CMD 
    

    will get you what you want