Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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 '12 at 16:42
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.

share|improve this answer

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
share|improve this answer
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 :) – stringbasic Apr 23 '12 at 16:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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