Skip to content
shellhacks.dev
Cron and Scheduling

Escape percent signs in a cron command

A percent sign in a crontab command ends the command and feeds the rest to stdin. Escape it with a backslash, or the date call below breaks at once.

1 min read
Summarize in AI

A cron job that works in a terminal fails every time it runs, and the only difference is date +%F in the command.

Shell
crontab - <<'EOF'
* * * * * date +%F > /tmp/bad.txt
* * * * * date +\%F > /tmp/good.txt
EOF
sleep 65
echo "bad:"; cat /tmp/bad.txt
echo "good:"; cat /tmp/good.txt
Text
bad:
cat: /tmp/bad.txt: No such file or directory
good:
2026-11-06

In a crontab line an unescaped percent sign is turned into a newline, and everything after the first one is sent to the command as standard input, which is how the crontab format is specified. The shell therefore ran only date +, and the redirection text F > /tmp/bad.txt became its input, so the file was never even created. Escaping the sign with a backslash passes it through.

The escape is needed in the crontab itself only. The same command in a script file called from cron needs no escape, which is a reason to keep anything with a percent sign out of the crontab. Not every cron treats the percent sign alike, so check the manual of the implementation in use.

Share

More snippets

Cron and Scheduling

Set PATH in a crontab

Set PATH on a line at the top of a crontab, because cron runs jobs with a minimal PATH of /usr/bin and /bin and a tool in /usr/local/bin is not found.

1 min read
Cron and Scheduling

Set the shell for cron

Cron runs jobs with /bin/sh. Set SHELL=/bin/bash at the top of the crontab to use Bash features such as [[ ]], arrays and process substitution.

1 min read
Cron and Scheduling

Send cron output to a file

Send cron output to a log with >> file 2>&1. The order matters: 2>&1 before the file leaves stderr behind, and cron mails or drops it.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.