Search code examples
unixcron

How to have an environment variable with quotes in crontab


I have a minimum example of my problem, which is trying to install a crontab that looks like this:

FOO="foo\"bar\"bin"

0 0 1 * * ls

by running crontab crontab (I have it in a file name crontab). It fails with this error:

% crontab crontab
"crontab":0: bad minute
crontab: errors in crontab file, can't install

It's the escaped quote, \", causing the problem.

How do I escape quotes in crontab's environment variables?

I'm not asking how to add environment variables to a shell script. I know how to do that. If you want to know why my question is different, here's the context: I'm not just writing variables in a manually written crontab. This crontab is generated by [whenever][1] (a Ruby gem), it has about 70 entries in them, and it picks up all environment variables and puts them as variables in the crontab so all credentials are available to the scripts. The problem is that recently I added a complex one that has quotes in it, and that broke this system.

In case you are curious, the Ruby code that generates the variables is:

ENV.each do |key, value|
  env key.to_sym, value.inspect
end

Solution

  • As far as I could find, crontab variables just cannot have quotes and they also can't be more than 999 characters long. The answer to this question is that it's simply not possible.