Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
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 '12 at 16:42

2 Answers 2

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
20120423170114
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

 
discard

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.