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 ?

link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

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.)

link|improve this answer
feedback

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".

link|improve this answer
1  
+1 to showing how the OP could figure out similar things on their own. – Arthaey Feb 9 at 22:02
feedback

No you can't a cronjob can only contain cron items. Is it not an option to create a bash-script and run the bash-script via a cronjob?

link|improve this answer
True on some other unices, but Linux's cron does support environment variable assignment. – Gilles Oct 3 '10 at 20:45
feedback

Your Answer

 
or
required, but never shown

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