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

  • 1
    Since your variables holds the string echo 2012042323524, the SQL command is CREATE DATABASE demoecho 2012042323524 CHAR... -- note the extraneous string "echo " Apr 23 '12 at 16:42

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
  • I think you can remove the quotes too, in this case at least.
    – Mat
    Apr 23 '12 at 16:21
  • Yes @Mat, dbnya=$(date +%Y%m%d%H%M%S) is enough :) Apr 23 '12 at 16:25

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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