Super User is a question and answer site for computer enthusiasts and power users. 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

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

You must log in to answer this question.

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