String expansion in bash with embedded quotes String expansion in bash with embedded quotes shell shell

String expansion in bash with embedded quotes


Because $TARG_DB is within single quotes within the subshell, it's taken literally. Use double quotes there, they won't mess up the subshell. e.g.

$ tmp="a b c"$ echo $tmpa b c$ echo $(echo $tmp)a b c$ echo $(echo "$tmp")a b c$ echo $(echo '$tmp')$tmp$ echo "$(echo '$tmp')"$tmp$ echo "$(echo "$tmp")"a b c


Don't wrap it in single-quotes.

There is no need to quote the command substitution $() so you can remove those and use double-quotes inside it.

DB_CREATE=$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse "create database $TARG_DB");


TARG_DB="zztest";DB_CREATE="$(${MYSQL} -u ${MyUSER} -h ${MyHOST} -p${MyPASS} -Bse \"create database ${TARG_DB}\")";