Bash command variable assignment

If I assign a variable:

testThis='echo "This is a test"'

If I use $testThis, it works in a script.

But, what if I want to skip a line ? So I try:

testThis='echo; echo "This is a test"'

and this fails!

Can't figure it out after much effort trying $() command substitution and all sorts of quoting.


A parameter expansion is not reparsed. The following

testThis='echo; echo "This is a test"'
$testThis

is equivalent to

echo ';' echo \"This is a test\"

The semicolon and the double quotes are both literal parts off the string, not shell syntax. After word-splitting and quote removal, the shell identifies the command echo with six arguments:

  1. ;
  2. echo
  3. "This
  4. is
  5. a
  6. test"

Use a function instead:

testThis () {
    echo; echo "This is a test"
}

testThis