0

I have read that we can determine the current date in Bash with the date command like follows:

NOW=$(date +"%Y-%m-%d")

In my crontab I append logs to files, and wanted to use this to put dates at the end of my file names, like follows:

cd /x/y/z/ && mycommand &>> /x/y/z/logs/mylog_$(date '+%Y%m%d').txt

However, this doesn't work as expected. I get the below error message, which I don't understand... I have no idea what it's referring to. I'm pretty new to using cron jobs.

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file
Stephen
  • 134
  • 6
  • 4
    Make things easier for you: Don't try to force everything into one line for cron. Instead, create a proper shell script and execute the script from cron. – Sven Apr 18 '18 at 20:38

1 Answers1

1

The percent character % is a cron's own special character. It is interpreted before the text is fed to shell (so it is interpreted withing single quotes, etc.). You need to escape it inside crontab with backslash \%

kubanczyk
  • 13,502
  • 5
  • 40
  • 55