2

Problem:

The following shell script code does not produce the expected result:

# MYSQL, MyUSER MyHost etc ... all defined above as normal

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

Expected outcome:

A new database created with the name zztest

Actual outcome:

A new database created with the name $TARG_DB

Question:

How can this example code be changed such that $TARG_DB is interpolated or expanded, giving the expected outcome?

Kevin
  • 53,822
  • 15
  • 101
  • 132
dreftymac
  • 31,404
  • 26
  • 119
  • 182

4 Answers4

7

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 $tmp
a 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
Kevin
  • 53,822
  • 15
  • 101
  • 132
  • Great answer, direct: with examples. Thanks. – dreftymac Feb 02 '12 at 16:18
  • Incidentally, Stack Overflow misinterprets the quotes (making the answer misleading) while a decent editor knows about the command substitution and highlights it correctly. – Cuadue Feb 03 '15 at 17:46
2

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");
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • Is this indeed true, that there is no difference between VAR="$(...)" and VAR=$(...) in bash? – macieksk Jun 09 '18 at 15:12
  • @macieksk It is NOT true. The entire RHS of the assignment expression should be wrapped in double quotes. It won't matter that another double-quoted string exists in the content; bash doesn't delimit nested strings in the same way as an imperative programming language. – Joe Coder Apr 06 '22 at 13:10
  • @JoeCoder it is 100% true when we are talking about `$( )`. Per https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html *”When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.”* – SiegeX Apr 07 '22 at 18:40
  • @SiegeX I meant in the outer context of being the RHS of an assignment statement. However I was wrong in thinking that string splitting or globbing was a concern, it turns out that is not the casee (https://stackoverflow.com/a/50528429/159570). No quotes necessary on RHS of simple assignment expression. – Joe Coder Apr 08 '22 at 00:09
0
TARG_DB="zztest";
DB_CREATE="$(${MYSQL} -u ${MyUSER} -h ${MyHOST} -p${MyPASS} -Bse \"create database ${TARG_DB}\")";
sgibb
  • 25,396
  • 3
  • 68
  • 74
0

The 'create database $TARG_DB' part is wrong! Single quotes supress variable substitution in bash. MySQL "sees" the literal text "$TARG_DB" and creates a database with that name. Put your sql statement in double quotes.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66