0

Inside of ~/.bashrc, I run a bash script (a script with #!/usr/bin/env bash at the top). I have found that when BASH_ENV is set to my ~/.bashrc that this sets up an infinite loop when a bash command is run. What is the best way to avoid this?

Here is my understanding of what happens (see also the bash manual on startup files):

  1. bash run non-interactively resolves BASH_ENV to ~/.bashrc.
  2. bash begins sourcing ~/.bashrc.
  3. bash gets to the line in ~/.bashrc that calls the bash script and starts a bash subprocess.
  4. The bash subprocess resolves BASH_ENV to ~/.bashrc and so goes back to step 1 here, setting up an infinite loop.

I came across this issue because I wanted to run some cron jobs using my standard environment and found suggestions to set BASH_ENV within the crontab. I am posting this as a question here because I couldn't find the issue discussed in my searches online and it seems like something could bite others. I am posting my own solution below, but there may be better ones.

1

At the top of .bashrc put:

unset BASH_ENV

If .bashrc is already being sourced to set up the environment, there is no need for future calls to bash to set up the environment again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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