How to pass environment variables to a non-interactive shell (with example)? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionLinux environment variable substitutionNone of the dot-files is sourced when running bash via ssh, part IIWhen is /etc/bash.bashrc invoked?How to store / load exported environment variables to / from a fileWhy does a non-interactve bash login shell ignore /etc/profile when invoked by su -?Are .bashrc and .zshrc the wrong place for environment variables?Non-interactive & non-login shell environment?Does a noninteractive login shell execute `~/.profile` or a file whose name is `$BASH_ENV`?How to set environment variables to path so it can be read from GUI and command line from the same program?Where is the PATH variable set when I start a Bash shell with env -i bash?Bash environment variables in non interactive mode

I'm having difficulty getting my players to do stuff in a sandbox campaign

Passing functions in C++

Using "nakedly" instead of "with nothing on"

Antler Helmet: Can it work?

When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?

How are presidential pardons supposed to be used?

Do we know why communications with Beresheet and NASA were lost during the attempted landing of the Moon lander?

How to rotate it perfectly?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?

Can the prologue be the backstory of your main character?

Biased dice probability question

When is phishing education going too far?

What is the electric potential inside a point charge?

Classification of bundles, Postnikov towers, obstruction theory, local coefficients

What kind of display is this?

Is there a service that would inform me whenever a new direct route is scheduled from a given airport?

Active filter with series inductor and resistor - do these exist?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Can smartphones with the same camera sensor have different image quality?

3 doors, three guards, one stone

What are the performance impacts of 'functional' Rust?

How should I respond to a player wanting to catch a sword between their hands?

What computer would be fastest for Mathematica Home Edition?



How to pass environment variables to a non-interactive shell (with example)?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionLinux environment variable substitutionNone of the dot-files is sourced when running bash via ssh, part IIWhen is /etc/bash.bashrc invoked?How to store / load exported environment variables to / from a fileWhy does a non-interactve bash login shell ignore /etc/profile when invoked by su -?Are .bashrc and .zshrc the wrong place for environment variables?Non-interactive & non-login shell environment?Does a noninteractive login shell execute `~/.profile` or a file whose name is `$BASH_ENV`?How to set environment variables to path so it can be read from GUI and command line from the same program?Where is the PATH variable set when I start a Bash shell with env -i bash?Bash environment variables in non interactive mode



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6















In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).



if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi


can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.



Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).




[1]I tried



  • BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

  • BASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

  • su -l artifactory -c echo $JAVA_HOME









share|improve this question






























    6















    In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).



    if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi


    can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.



    Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).




    [1]I tried



    • BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

    • BASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

    • su -l artifactory -c echo $JAVA_HOME









    share|improve this question


























      6












      6








      6


      3






      In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).



      if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi


      can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.



      Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).




      [1]I tried



      • BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

      • BASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

      • su -l artifactory -c echo $JAVA_HOME









      share|improve this question
















      In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).



      if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi


      can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.



      Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).




      [1]I tried



      • BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

      • BASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOME

      • su -l artifactory -c echo $JAVA_HOME






      bash environment-variables scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 17 hours ago









      Rui F Ribeiro

      42.1k1483142




      42.1k1483142










      asked Sep 29 '14 at 1:09









      Karl RichterKarl Richter

      8001823




      8001823




















          1 Answer
          1






          active

          oldest

          votes


















          5














          If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:



          ENV=/path/to/rcfile sh


          Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:



          export "PATH=$PATH:/some/more/paths"; sh


          The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:



          #!/bin/bash


          ...or whatever bang line.



          Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:



          env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file


          That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.






          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',
            autoActivateHeartbeat: false,
            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%2f158117%2fhow-to-pass-environment-variables-to-a-non-interactive-shell-with-example%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









            5














            If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:



            ENV=/path/to/rcfile sh


            Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:



            export "PATH=$PATH:/some/more/paths"; sh


            The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:



            #!/bin/bash


            ...or whatever bang line.



            Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:



            env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file


            That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.






            share|improve this answer





























              5














              If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:



              ENV=/path/to/rcfile sh


              Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:



              export "PATH=$PATH:/some/more/paths"; sh


              The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:



              #!/bin/bash


              ...or whatever bang line.



              Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:



              env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file


              That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.






              share|improve this answer



























                5












                5








                5







                If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:



                ENV=/path/to/rcfile sh


                Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:



                export "PATH=$PATH:/some/more/paths"; sh


                The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:



                #!/bin/bash


                ...or whatever bang line.



                Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:



                env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file


                That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.






                share|improve this answer















                If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:



                ENV=/path/to/rcfile sh


                Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:



                export "PATH=$PATH:/some/more/paths"; sh


                The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:



                #!/bin/bash


                ...or whatever bang line.



                Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:



                env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file


                That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 29 '14 at 1:40

























                answered Sep 29 '14 at 1:16









                mikeservmikeserv

                46.1k669164




                46.1k669164



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f158117%2fhow-to-pass-environment-variables-to-a-non-interactive-shell-with-example%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







                    -bash, environment-variables, scripting

                    Popular posts from this blog

                    Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

                    Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

                    Can I redirect output to a log file and background a process at the same time?2019 Community Moderator ElectionUnable to write to file in shell script when running command in backgroundredirect and log script outputhow to properly log the output of a console program that frequently updates “parts” of the screen, resulting in a messy log file?How can I redirect the output of a child process?grep script - output lines at the same time into echoHow to run process in background and get its pid to create log file nameHow to redirect output to a log from expect commandHow to clear a redirect log file content in shell?Log background jobs started in all the manually started shells?Why do `jobs` and `dirs` run in command subsitution, process substitution, pipeline, and background jobs output the same as in original shell?How to redirect output to file to STDOUT?