Is it possible to use variables in crontab -e?

Can I say:

MYPATH=/root/scripts  
MYSCRIPT=doit.sh

0 1 * * * $MYPATH/$MYSCRIPT

in crontab -e?

Is it possible to use variables in crontab -e?


Yes, you can define and use variables in this way. There's a limitation (which you haven't hit in your examples): the string on the right of the = sign is interpreted literally, with leading spaces removed, so you can't use constructs like FOO=$BAR/qux (e.g. PATH=$HOME/bin:$PATH won't do anything useful).

This is stated in the documentation, which you can see by running

man 5 crontab

(Note that man crontab shows the documentation of the crontab command, in section 1 of the manual; you want the documentation of the crontab file format, in section 5.)


Just made a try, yes it's possible. You can figure it out with this simple example, put this in your crontab:

FOO=qwerty
* * * * * echo $FOO > ~/out

And check the file ~/out (updated every minute), it should contain "qwerty".