I want to complement ravi's answer:
This behavior is specific to Ubuntu (and probably most derived distros), since your default
~/.bashrc file starts with a short-circuit, Ubuntu 18.04, for example:
# If not running interactively, don't do anything
case $- in
That will stop the evaluation of the file if it is running in a non-interactive shell, which is the case of your script since all scripts are run in a non-interactive shell, and subsequently every file you
source will inherit this property.
I found out an ugly hack to workaround Ubuntu specifically, using
eval instead of
eval "$(cat ~/.bashrc | tail -n +10)"
It simply skips the few first lines and evaluates the rest of the
~/.bashrc so the rest is evaluated and modifies the current execution.
Be aware it is a magic number and might not work across Ubuntu versions; but may be a good solution if your are crafting scripts for more-or-less known systems.
A fancier solution might involve using regex to target the specific bits that stop the evaluation.
Another alternative that might work better in some scenarios is forcing the script to run in an interactive shell by adding a flag in the shebang:
Be aware of a few things: