for the variable

dbnya="echo $(date +%Y%m%d%H%M%S)"

When running below code, I will get an error (SQL syntax error)

mysql -u root -pthepass -e "CREATE DATABASE demo$dbnya CHARACTER SET utf8 COLLATE utf8_bin"

The second command will work if I'm using dbnya=20120423230524 as first command

link|improve this question
1  
Since your variables holds the string echo 2012042323524, the SQL command is CREATE DATABASE demoecho 2012042323524 CHAR... -- note the extraneous string "echo " – glenn jackman Apr 23 at 16:42
feedback

2 Answers

up vote 1 down vote accepted

Use backticks instead of quotes.

dbnya=`echo $(date +%Y%m%d%H%M%S)`

The backtick (`) indicates the text is to be executed as a command, thus setting the variable to the output of the command.

link|improve this answer
feedback

You don't need to use the echo command:

$> dbnya="echo $(date +%Y%m%d%H%M%S)"
$> echo $dbnya
echo 20120423170042

Just remove it from the command, dbnya will receive the result of the date evaluation:

$> dbnya="$(date +%Y%m%d%H%M%S)"
$> echo $dbnya
20120423170114
link|improve this answer
I think you can remove the quotes too, in this case at least. – Mat Apr 23 at 16:21
Yes @Mat, dbnya=$(date +%Y%m%d%H%M%S) is enough :) – stringbasic Apr 23 at 16:25
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.