Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have the following excerpt from a bash script used to backup a database

FILENAME=$DB_$(date +%s).sql

I am trying to reuse the value of DB within the FILENAME variable assignment, but it won't let me use substitution this way. I just get the timestamp for the file name.

Is it possible to achieve what I want, and if so what is the syntax?


share|improve this question

3 Answers 3

up vote 8 down vote accepted

The problem is that bash does not know that you mean $DB instead of $DB_ (which is a perfectly valid name for a variable).

The best option is to be explicit on the name of the variable using braces around its name:

FILENAME=${DB}_$(date %s).sql

This saves you the trouble of escaping other characters which are not to be interpreted as part of a variable name.

share|improve this answer
I ended up accepting this answer as I found it more complete. I also find the braces to be more readable. Thanks. –  LukeR Sep 8 '11 at 5:27

Alternately, use braces to isolate your variable. I think it's a bit clearer than putting quotes in there.

FILENAME=${DB}_$(date +%s).sql
share|improve this answer
Also works within quotes: FILENAME="${DB}_$(date +%s)" if you want to embed stars or spaces or something. –  tylerl Sep 8 '11 at 4:15

Enclose the underscore in double quotes (or single quotes for the stronger):

FILENAME=$DB"_"$(date +%s).sql
share|improve this answer
Legend. It won't let me accept this for 9 minutes, but you have made me a momentarily happy man. Cheers. –  LukeR Sep 8 '11 at 4:05

Your Answer


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.