source ~/.bashrc often fails when called from an non-interactive script, because some platforms have a check within ~/.bashrc

From stackoverflow:

Some platforms come with a ~/.bashrc that has a conditional at the top > that explicitly stops processing if the shell is found to be > non-interactive.

If this is the case, sourcing ~/.bashrc from a script will have no effect, > because scripts run in non-interactive shells by default.

Your options are:

  • Either: deactivate the conditional in ~/.bashrc

  • Or: Try to to emulate an interactive shell before invoking source > ~/.bashrc. The specific emulation needed depends on the specifics of the > conditional, but there are two likely approaches; you may have to > employ them both if you don’t know ahead of time which conditional > you’ll encounter: set -i temporarily to make $- contain i, indicating an interactive > shell. Make sure that environment variable PS1 has a value.

Alternatively, if you control how your own script is invoked, you can > invoke it with bash -i script.

bash - source .bashrc in a script not working - Stack Overflow