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?










share|improve this question


















  • 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










  • 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












  • Ah, yes. That's the case here as well.
    – pir
    Nov 15 at 2:05






  • 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















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?










share|improve this question


















  • 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










  • 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












  • Ah, yes. That's the case here as well.
    – pir
    Nov 15 at 2:05






  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 at 23:02









pir

1053




1053








  • 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










  • 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












  • Ah, yes. That's the case here as well.
    – pir
    Nov 15 at 2:05






  • 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














  • 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










  • 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












  • Ah, yes. That's the case here as well.
    – pir
    Nov 15 at 2:05






  • 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








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










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).






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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).






    share|improve this answer

























      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).






      share|improve this answer























        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).






        share|improve this answer












        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 6:59









        Kusalananda

        116k15218351




        116k15218351






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts