Stack Exchange Network
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
The best answers are voted up and rise to the top
I have the following excerpt from a bash script used to backup a database
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?
asked Sep 8, 2011 at 3:58
3,12622 gold badges2929 silver badges2525 bronze badges
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:
This saves you the trouble of escaping other characters which are not to be interpreted as part of a variable name.
answered Sep 8, 2011 at 4:16
2,3541818 silver badges1010 bronze badges
Alternately, use braces to isolate your variable. I think it's a bit clearer than putting quotes in there.
answered Sep 8, 2011 at 4:13
15k77 gold badges5151 silver badges7272 bronze badges
Enclose the underscore in double quotes (or single quotes for the stronger):
answered Sep 8, 2011 at 4:02
51.3k1919 gold badges159159 silver badges217217 bronze badges