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?


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.

  • 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

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.