I'm working on a bash script that backs up a configuration file before copying over a new file.

Here's what my snippet looks like:

mv ~/myStuff.conf  ~/myStuff.conf.bak
cp ~/new/myStuff.conf ~/myStuff.conf

Every time this script is run, I'd like there the backup to have a unix timestamp in the filename. I tried this

DATEVAR=date +%s
mv ~/myStuff.conf  ~/myStuff.conf.$DATEVAR.bak

But this doesn't work, since the date function doesn't execute and bash sees it as a string, and the resulting file ends up being

myStuff.conf.date+%s.bak

Any ideas on how to get the results of the date function into a variable?

link|improve this question

80% accept rate
feedback

1 Answer

up vote 2 down vote accepted

This is possible with command substitution.

DATEVAR=$(date +%s)
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.