In bash, how do I interpolate $(...) in a string?

Hoytman picture Hoytman · May 14, 2014 · Viewed 21.1k times · Source

I am attempting to write a bash script that performs a mysqldump on my live site's database, then adds and commits the dump to a git repository. Here is what I have so far (stored in a .sh file which is called by a crontab entry):

/usr/bin/mysqldump --skip-comments --skip-dump-date -u [user] -p[pass] database | gzip > /var/www/site/backup/database.sql.gz
cd var/www/site/backup && git add *
cd var/www/site/backup && git commit -m 'Database $(date +%a %H:%M %h %d %Y)'

My crontab entry looks like this:

0,20,40 8-22 * * * /var/www/site/backup/script.sh

I can see that this script does dump the database, but does not add or commit the file to git. Is there something that I am missing?

Edit =================================

I made the following changes and the commit works:

cd /var/www/site/backup && /usr/bin/git add *
cd /var/www/site/backup && /usr/bin/git commit -m 'Database $(date +%a %H:%M %h %d %Y)'

However, the date does not get calculated.

Edit =================================

Latest Revisions, including (most of) the recommendations

/usr/bin/mysqldump --skip-comments --skip-dump-date -u [user] -p[pass] database > /var/www/site/backup/database.sql
cd var/www/site/backup
/usr/bin/git add *
/usr/bin/git commit -m "Internal Forms Live Database Dump Stored $(date '+%a %H:%M %h %d %Y')"

Answer

kostix picture kostix · May 14, 2014

$(...) and other forms of substitutions are not interpolated in single-quoted strings.

So if you want your date calculated, do

git commit -m "Database $(date '+%a %M:%H %h %d %Y')"

that is, the whole message string is double-quoted to allow $(...) to be interpolated while the argument to date is in single quotes to make it a single argument (passed to date).