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

                    What is this called? Old film camera viewer?What makes a good film camera?What to do with an old film camera?What should one look for when buying a used film camera?What is the value and age of this pre-1967 Ricoh 35 mm camera?DSLR recommendation, question about old Canon 35mm film Camera & lensesCan anyone identify the silver rangefinder-style camera in this advertisement?What kind of a Polaroid 600-camera is this?Will an old film camera still work even when not used in a very long time?What is this camera / Can I develop the film?How to fit an action camera into antique (bellows) housing?What to check when buying used and old film bodies?

                    Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?