To be more specific, I want the Unix timestamp to be appended to my file name:

db:backup --database=mysql --destination=s3 --destinationPath=date+random.sql --compression=gzip

However, this gives me "date+random.sql" as the filename, which is not what I want.


Use the $( ... ) construct to create a textual output of a command ... substituted; as in...


But note that the standard date format is not well suited for timestamps, so you'd typically define a more appropriate format, as in..

--destinationPath=$(date +%Y%m%d%H%M%S)+random.sql
  • Please disregard my answer if it still appears. The above is correct. see tldp.org/LDP/abs/html/commandsub.html – iyrin Mar 12 '15 at 22:31
  • $(date +%s) provides a Unix time timestamp. This is my favorite for archiving purposes. – Antxon Mar 13 '15 at 5:41
  • I think the OP tried to use + as a concatenation operator, but did not actually intend to get it inside the file name :) – John WH Smith Mar 17 '15 at 17:29
  • @John; I suppose so, but having some separator is not bad, I'd say. I also suspect that the OP does not need a literal string "random", but actually something like --destinationPath=$(date +%Y%m%d%H%M%S)_$RANDOM.sql. (He surely found that out himself. :) – Janis Mar 17 '15 at 17:34
  • That doesn't fully work for cron jobs on Ubuntu, had to append \ to escape – Ali Gajani Mar 21 '15 at 10:26

I also like --destinationPath=$(date +%F)+random.sql

which gives a date like 2015-03-17.

I believe the date formats are Posix formats used in strftime().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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