1

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?

1 Answer 1

4

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.

4
  • I would use "${db}_${DATE}" to avoid any variable substitution issues.
    – jftuga
    Aug 12, 2011 at 12:28
  • What issues do you mean?
    – jw013
    Aug 12, 2011 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, 2011 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, 2011 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .