Bash – How to guarantee availability of $BASH_ENV

bashenvironment-variables

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?

Best Answer

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
…