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

bash, shell, string-interpolation

Solution

`$(...)` 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`).

Problem

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? Later 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. 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')" ```

Original source