Sourcing .bashrc inside script doesn't update the env variables
up vote
0
down vote
favorite
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?
bash shell-script environment-variables
add a comment |
up vote
0
down vote
favorite
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?
bash shell-script environment-variables
2
What shell are you executing these commands in? Thedashshell (/bin/shon some Linuxes) does not havesource. Also, if any code in~/.bashrcstops 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.
– Kusalananda
Nov 14 at 23:09
I'm using#!/bin/bash. Interesting idea that the file might stop. It's just the plain.bashrcfile generated by Ubuntu 16.04 with 4 exports for env vars.
– pir
Nov 14 at 23:50
at least on debian, the default ~/.bashrc starts with something likecase $- in *i*) ;; *) return ;; esac. So, if the shell is not interactive,source ~/.bashrcwill return immediately, and any lines you may add at the end have no effect.
– mosvy
Nov 15 at 0:35
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
1
I'll have a separate.bash_myenvfile and then just source that instead, and add a line to.bashrcto source that one. Thanks! Feel free to provide your comment as an answer.
– pir
Nov 15 at 2:06
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
bash shell-script environment-variables
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?
bash shell-script environment-variables
bash shell-script environment-variables
asked Nov 14 at 23:02
pir
1053
1053
2
What shell are you executing these commands in? Thedashshell (/bin/shon some Linuxes) does not havesource. Also, if any code in~/.bashrcstops 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.
– Kusalananda
Nov 14 at 23:09
I'm using#!/bin/bash. Interesting idea that the file might stop. It's just the plain.bashrcfile generated by Ubuntu 16.04 with 4 exports for env vars.
– pir
Nov 14 at 23:50
at least on debian, the default ~/.bashrc starts with something likecase $- in *i*) ;; *) return ;; esac. So, if the shell is not interactive,source ~/.bashrcwill return immediately, and any lines you may add at the end have no effect.
– mosvy
Nov 15 at 0:35
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
1
I'll have a separate.bash_myenvfile and then just source that instead, and add a line to.bashrcto source that one. Thanks! Feel free to provide your comment as an answer.
– pir
Nov 15 at 2:06
add a comment |
2
What shell are you executing these commands in? Thedashshell (/bin/shon some Linuxes) does not havesource. Also, if any code in~/.bashrcstops 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.
– Kusalananda
Nov 14 at 23:09
I'm using#!/bin/bash. Interesting idea that the file might stop. It's just the plain.bashrcfile generated by Ubuntu 16.04 with 4 exports for env vars.
– pir
Nov 14 at 23:50
at least on debian, the default ~/.bashrc starts with something likecase $- in *i*) ;; *) return ;; esac. So, if the shell is not interactive,source ~/.bashrcwill return immediately, and any lines you may add at the end have no effect.
– mosvy
Nov 15 at 0:35
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
1
I'll have a separate.bash_myenvfile and then just source that instead, and add a line to.bashrcto source that one. Thanks! Feel free to provide your comment as an answer.
– pir
Nov 15 at 2:06
2
2
What shell are you executing these commands in? The
dash shell (/bin/sh on some Linuxes) does not have source. 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.– Kusalananda
Nov 14 at 23:09
What shell are you executing these commands in? The
dash shell (/bin/sh on some Linuxes) does not have source. 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.– Kusalananda
Nov 14 at 23:09
I'm using
#!/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.– pir
Nov 14 at 23:50
I'm using
#!/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.– pir
Nov 14 at 23:50
at least on debian, the default ~/.bashrc starts with something like
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.– mosvy
Nov 15 at 0:35
at least on debian, the default ~/.bashrc starts with something like
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.– mosvy
Nov 15 at 0:35
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
1
1
I'll have a separate
.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.– pir
Nov 15 at 2:06
I'll have a separate
.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.– pir
Nov 15 at 2:06
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Your ~/.bashrc file detects whether it's being sourced by an interactive or non-interactive shell:
case $- in
*i*) ;;
*) return ;;
esac
When this file is sourced from a script (which is a non-interactive shell), the return branch is taken and the file does not execute to the end.
Adding export statements to the end of the file would mean that these would not be executed when sourced from a script.
The solution may be to write the export statements to a separate file and source that from your script (and possibly from ~/.bashrc as well if you think its needed).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your ~/.bashrc file detects whether it's being sourced by an interactive or non-interactive shell:
case $- in
*i*) ;;
*) return ;;
esac
When this file is sourced from a script (which is a non-interactive shell), the return branch is taken and the file does not execute to the end.
Adding export statements to the end of the file would mean that these would not be executed when sourced from a script.
The solution may be to write the export statements to a separate file and source that from your script (and possibly from ~/.bashrc as well if you think its needed).
add a comment |
up vote
2
down vote
accepted
Your ~/.bashrc file detects whether it's being sourced by an interactive or non-interactive shell:
case $- in
*i*) ;;
*) return ;;
esac
When this file is sourced from a script (which is a non-interactive shell), the return branch is taken and the file does not execute to the end.
Adding export statements to the end of the file would mean that these would not be executed when sourced from a script.
The solution may be to write the export statements to a separate file and source that from your script (and possibly from ~/.bashrc as well if you think its needed).
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your ~/.bashrc file detects whether it's being sourced by an interactive or non-interactive shell:
case $- in
*i*) ;;
*) return ;;
esac
When this file is sourced from a script (which is a non-interactive shell), the return branch is taken and the file does not execute to the end.
Adding export statements to the end of the file would mean that these would not be executed when sourced from a script.
The solution may be to write the export statements to a separate file and source that from your script (and possibly from ~/.bashrc as well if you think its needed).
Your ~/.bashrc file detects whether it's being sourced by an interactive or non-interactive shell:
case $- in
*i*) ;;
*) return ;;
esac
When this file is sourced from a script (which is a non-interactive shell), the return branch is taken and the file does not execute to the end.
Adding export statements to the end of the file would mean that these would not be executed when sourced from a script.
The solution may be to write the export statements to a separate file and source that from your script (and possibly from ~/.bashrc as well if you think its needed).
answered Nov 15 at 6:59
Kusalananda
116k15218351
116k15218351
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481816%2fsourcing-bashrc-inside-script-doesnt-update-the-env-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What shell are you executing these commands in? The
dashshell (/bin/shon some Linuxes) does not havesource. Also, if any code in~/.bashrcstops 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.– Kusalananda
Nov 14 at 23:09
I'm using
#!/bin/bash. Interesting idea that the file might stop. It's just the plain.bashrcfile generated by Ubuntu 16.04 with 4 exports for env vars.– pir
Nov 14 at 23:50
at least on debian, the default ~/.bashrc starts with something like
case $- in *i*) ;; *) return ;; esac. So, if the shell is not interactive,source ~/.bashrcwill return immediately, and any lines you may add at the end have no effect.– mosvy
Nov 15 at 0:35
Ah, yes. That's the case here as well.
– pir
Nov 15 at 2:05
1
I'll have a separate
.bash_myenvfile and then just source that instead, and add a line to.bashrcto source that one. Thanks! Feel free to provide your comment as an answer.– pir
Nov 15 at 2:06