Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have $db and $DATE set in my bash script, then I need to join them like this:

mysqldump --user=usr --password=pss --databases $db | gzip > /backups/sqlNew/$db_$DATE.sql.gz;

Unfortunately, that doesn't work. How do I properly join those 2 variables into a filename?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Since _ is a valid character in a variable name you have to separate it from db. This can be done any number of ways.

  • "${db}_$DATE" is one.
  • "$db"_"$DATE" would be another one.

Always remember to quote your variables when writing bash scripts.

share|improve this answer
    
I would use "${db}_${DATE}" to avoid any variable substitution issues. –  jftuga Aug 12 '11 at 12:28
    
What issues do you mean? –  jw013 Aug 12 '11 at 12:37
    
For example, $DATE_OLD vs. ${DATE}_OLD You are probably OK here, but just something to be aware of for other scripts. –  jftuga Aug 12 '11 at 15:00
    
Ok that's what you meant -- since the original question had a . after DATE I left that out since the focus was on the db_ part. –  jw013 Aug 12 '11 at 15:33

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.