A non-interactive, non-login shell will try to source any script specified in $BASH_ENV. But how do I guarantee $BASH_ENV is set before a cron job or script has a chance to set $BASH_ENV for any particular session? Is the only option to compile Bash with it hardcoded?
If you want all the bash scripts in your crontab to load
BASH_ENV, set it at the crontab level.
BASH_ENV=/path/to/startup.bash 12 34 * * * /path/to/bash_script 1 23 1 * * /path/to/other_bash_script
If you want to set
BASH_ENV only for a particular entry, set it there. Then
BASH_ENV won't be set for the code listed in the crontab itself, but it's a bad idea to put anything complex there anyway.
12 34 * * * export BASH_ENV=/path/to/startup.bash; /path/to/bash_script 1 23 1 * * /path/to/other_bash_script
If you want a particular script to always load some configuration file, load it directly from within the script.
#!/bin/bash . /path/to/configuration.bash …
BASH_ENV is only read in a non-interactive shell, and only if that shell is bash (and not called under the name
sh, either). A non-login, interactive shell does not look for
$ export BASH_ENV=/home/cuonglm/bash-env.sh $ bash -lci '. test.sh' QWERTY $ bash -lc '. test.sh' BASH_ENV read QWERTY $ bash -ci '. test.sh' QWERTY $ bash -c '. test.sh' BASH_ENV read QWERTY
There is no standard file that is run in non-interactive shell for user. You should set it in separated file then source it:
bash -c '. ~/.profile; echo 123'
Or you can set it in some system wide config file like