I have a long installation script that frequently inserts env variables into .bashrc for immediate and future use, then sources .bashrc
to update the env variables. However, this is not working as intended. Unlike when run independently in terminal, running these commands together as a script fails to update the current environment.
Here's a small example:
echo export TEST_BASH=sup >> ~/.bashrc
source ~/.bashrc
echo $TEST_BASH
The final line will print empty rather than 'sup' as intended. Why is that?
dash
shell (/bin/sh
on some Linuxes) does not havesource
. Also, if any code in~/.bashrc
stops the sourcing of the file to the end (possibly depending on whether it's being sourced by an interactive shell or not), the variable would obviously not be set.#!/bin/bash
. Interesting idea that the file might stop. It's just the plain.bashrc
file generated by Ubuntu 16.04 with 4 exports for env vars.case $- in *i*) ;; *) return ;; esac
. So, if the shell is not interactive,source ~/.bashrc
will return immediately, and any lines you may add at the end have no effect..bash_myenv
file and then just source that instead, and add a line to.bashrc
to source that one. Thanks! Feel free to provide your comment as an answer.