-1

In Ubuntu 16.04 Bash 4.3.46(1) at the end of /etc/bash.bashrc I declared the following variable:

drt="/var/www/html"

I sourced the file (source /etc/bash.bashrc)

I executed crontab -e and added just for testing * * * * * touch /var/www/html/myFile.sh. This worked, while * * * * * touch ${drt}/myFile.sh didn't.

If a variable declared in bash.bashrc is global why it doesn't get processed by crontab?

Update1

I don't want to duplicate the stream drt="/var/www/html" into crontab (or cron.d) to avoid redundancy by principle.

Update2

I followed this session and it seems the answer marked as correct didn't solve my problem.

The second answer was somewhat unclear to me - why to put this conditional statement in (some?) bashrc file.

I also tried the third solution in relation to bash.bashrc (added SHELL=/bin/bash into the very start of crontab, saved the file and sourced bash.bashrc but had no change in a * * * * * touch ~/myfiile test.

I thus ask to reopen this question, or at least, clarify one the answers there.

0

1 Answer 1

2

The variables defined in *.bashrc are global only in the sense that they can be read out by programs running in the shell, and that they are handled specially by the shell. Whether a program reads these environmental variables and how they interpret them is completely dependent on the design and there is not regulation stating how a program should handle environment variables.

In short, the way cron interprets the crontab is decided by the program cron itself. It decides not to read the "global" environment variables and use its own syntax to define the "environment variables" instead. That's by design. If you can understand why we cannot just use ${drt} in a CMakeLists.txt, you should be able to understand why ${drt} in a crontab does not do what you expect it to do.

You can, of course, add the following line to your crontab if you wish.

drt="/var/www/html"
1
  • 2
    This isn't entirely true. bashrc is only read by interactive shells, that's why cron doesn't use it.
    – jordanm
    Jan 15, 2018 at 15:03

Not the answer you're looking for? Browse other questions tagged .