Why are environment variables different with `bash -c`Recursively rename files using find and sedWhy does rc.local require absolute paths? How can I run a script at startup that doesn't use them?Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediatelyHow can I run a full OS in a Docker container, without specifying a command?How to prevent attach or exec in a docker containerdocker: Percona how to populate mysql server with initial dataWhy do we use a OS Base Image with Docker if containers have no Guest OS?Configure php-fpm to access environment variables in dockerStarting specific task containers from inside a coordinator containerwhy sendmail in container not working when running on different host machines?

Why isn't Tyrion mentioned in the in-universe book "A Song of Ice and Fire"?

What were the Ethiopians doing in Xerxes' army?

Can you still travel to America on the ESTA waiver program if you have been to Iran in transit?

Is there an idiom that means that you are in a very strong negotiation position in a negotiation?

A burglar's sunglasses, a lady's odyssey

What are nvme namespaces? How do they work?

Why would a rational buyer offer to buy with no conditions precedent?

What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?

Why does the hash of infinity have the digits of π?

How to let other coworkers know that I don't share my coworker's political views?

Shorten or merge multiple lines of `&> /dev/null &`

Is superuser the same as root?

Why did it take so long for Germany to allow electric scooters / e-rollers on the roads?

xcolor breaking ligatures

The Maltese Falcon

Co-author wants to put their current funding source in the acknowledgements section because they edited the paper

Why sampling a periodic signal doesn't yield a periodic discrete signal?

How to determine if a hyphen (-) exists inside a column

Why is 'additive' EQ more difficult to use than 'subtractive'?

Time complexity of an algorithm: Is it important to state the base of the logarithm?

What did the 'turbo' button actually do?

Testing using real data of the customer

Does French have the English "short i" vowel?

Need to read my home electrical Meter



Why are environment variables different with `bash -c`


Recursively rename files using find and sedWhy does rc.local require absolute paths? How can I run a script at startup that doesn't use them?Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediatelyHow can I run a full OS in a Docker container, without specifying a command?How to prevent attach or exec in a docker containerdocker: Percona how to populate mysql server with initial dataWhy do we use a OS Base Image with Docker if containers have no Guest OS?Configure php-fpm to access environment variables in dockerStarting specific task containers from inside a coordinator containerwhy sendmail in container not working when running on different host machines?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















How come the following, i.e. echoing $PATH directly from bash -c:



docker exec -i -t my_container bash -c "echo $PATH"


returns a different value for $PATH than what follows, i.e. starting an interactive bash session and echoing the $PATH?



docker exec -i -t my_container bash 
root@21e6d898c3c2:/# echo $PATH


To give some context to this question, I'd like to run a command in the container with docker exec, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.



Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH are set in a bash interactive session, but not if I run the command up straight.










share|improve this question






















  • In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

    – Gordon Davisson
    Nov 25 '16 at 3:30

















6















How come the following, i.e. echoing $PATH directly from bash -c:



docker exec -i -t my_container bash -c "echo $PATH"


returns a different value for $PATH than what follows, i.e. starting an interactive bash session and echoing the $PATH?



docker exec -i -t my_container bash 
root@21e6d898c3c2:/# echo $PATH


To give some context to this question, I'd like to run a command in the container with docker exec, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.



Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH are set in a bash interactive session, but not if I run the command up straight.










share|improve this question






















  • In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

    – Gordon Davisson
    Nov 25 '16 at 3:30













6












6








6


1






How come the following, i.e. echoing $PATH directly from bash -c:



docker exec -i -t my_container bash -c "echo $PATH"


returns a different value for $PATH than what follows, i.e. starting an interactive bash session and echoing the $PATH?



docker exec -i -t my_container bash 
root@21e6d898c3c2:/# echo $PATH


To give some context to this question, I'd like to run a command in the container with docker exec, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.



Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH are set in a bash interactive session, but not if I run the command up straight.










share|improve this question














How come the following, i.e. echoing $PATH directly from bash -c:



docker exec -i -t my_container bash -c "echo $PATH"


returns a different value for $PATH than what follows, i.e. starting an interactive bash session and echoing the $PATH?



docker exec -i -t my_container bash 
root@21e6d898c3c2:/# echo $PATH


To give some context to this question, I'd like to run a command in the container with docker exec, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.



Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH are set in a bash interactive session, but not if I run the command up straight.







bash docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '16 at 19:14









Alessandro VernetAlessandro Vernet

222137




222137












  • In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

    – Gordon Davisson
    Nov 25 '16 at 3:30

















  • In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

    – Gordon Davisson
    Nov 25 '16 at 3:30
















In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

– Gordon Davisson
Nov 25 '16 at 3:30





In addition to what @chicks pointed out, using docker ... "echo $PATH" will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH') to prevent premature evaluation of the PATH variable.

– Gordon Davisson
Nov 25 '16 at 3:30










3 Answers
3






active

oldest

votes


















4














When -c is specified bash is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would definitely be skipped.



Also, as explained by the bash man page:




Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.




So if it doesn't think you're connecting across the network it might not read the .bashrc file either which would skip everything not skipped by the previous step.



solution



To work around this issue I would create a script that sets the PATH to something suitable and then run the command. If you want to use the existing .profile or other files then you can just source it in your script.






share|improve this answer


















  • 2





    why not run with bash -l -c "command"

    – Max Ivak
    Nov 22 '17 at 21:23


















5














In your first example:



docker exec -i -t my_container bash -c "echo $PATH"


That will evaluate the $PATH variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH on the command line outside of docker and see that they are the same.



In your second example:



docker exec -i -t my_container bash 
root@21e6d898c3c2:/# echo $PATH


That will evaluate the $PATH variable inside the container.



You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:



docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'





share|improve this answer


















  • 1





    Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

    – dave_thompson_085
    May 10 at 3:48



















1














Try the -l option for bash. It will run in login shell and load /etc/profile.



docker exec -i -t my_container bash -lc "echo $PATH"





share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "2"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fserverfault.com%2fquestions%2f816709%2fwhy-are-environment-variables-different-with-bash-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    When -c is specified bash is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would definitely be skipped.



    Also, as explained by the bash man page:




    Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.




    So if it doesn't think you're connecting across the network it might not read the .bashrc file either which would skip everything not skipped by the previous step.



    solution



    To work around this issue I would create a script that sets the PATH to something suitable and then run the command. If you want to use the existing .profile or other files then you can just source it in your script.






    share|improve this answer


















    • 2





      why not run with bash -l -c "command"

      – Max Ivak
      Nov 22 '17 at 21:23















    4














    When -c is specified bash is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would definitely be skipped.



    Also, as explained by the bash man page:




    Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.




    So if it doesn't think you're connecting across the network it might not read the .bashrc file either which would skip everything not skipped by the previous step.



    solution



    To work around this issue I would create a script that sets the PATH to something suitable and then run the command. If you want to use the existing .profile or other files then you can just source it in your script.






    share|improve this answer


















    • 2





      why not run with bash -l -c "command"

      – Max Ivak
      Nov 22 '17 at 21:23













    4












    4








    4







    When -c is specified bash is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would definitely be skipped.



    Also, as explained by the bash man page:




    Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.




    So if it doesn't think you're connecting across the network it might not read the .bashrc file either which would skip everything not skipped by the previous step.



    solution



    To work around this issue I would create a script that sets the PATH to something suitable and then run the command. If you want to use the existing .profile or other files then you can just source it in your script.






    share|improve this answer













    When -c is specified bash is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would definitely be skipped.



    Also, as explained by the bash man page:




    Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.




    So if it doesn't think you're connecting across the network it might not read the .bashrc file either which would skip everything not skipped by the previous step.



    solution



    To work around this issue I would create a script that sets the PATH to something suitable and then run the command. If you want to use the existing .profile or other files then you can just source it in your script.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '16 at 21:43









    chickschicks

    3,08572033




    3,08572033







    • 2





      why not run with bash -l -c "command"

      – Max Ivak
      Nov 22 '17 at 21:23












    • 2





      why not run with bash -l -c "command"

      – Max Ivak
      Nov 22 '17 at 21:23







    2




    2





    why not run with bash -l -c "command"

    – Max Ivak
    Nov 22 '17 at 21:23





    why not run with bash -l -c "command"

    – Max Ivak
    Nov 22 '17 at 21:23













    5














    In your first example:



    docker exec -i -t my_container bash -c "echo $PATH"


    That will evaluate the $PATH variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH on the command line outside of docker and see that they are the same.



    In your second example:



    docker exec -i -t my_container bash 
    root@21e6d898c3c2:/# echo $PATH


    That will evaluate the $PATH variable inside the container.



    You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:



    docker exec -i -t my_container bash -c "echo $PATH"
    docker exec -i -t my_container bash -c 'echo $PATH'





    share|improve this answer


















    • 1





      Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

      – dave_thompson_085
      May 10 at 3:48
















    5














    In your first example:



    docker exec -i -t my_container bash -c "echo $PATH"


    That will evaluate the $PATH variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH on the command line outside of docker and see that they are the same.



    In your second example:



    docker exec -i -t my_container bash 
    root@21e6d898c3c2:/# echo $PATH


    That will evaluate the $PATH variable inside the container.



    You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:



    docker exec -i -t my_container bash -c "echo $PATH"
    docker exec -i -t my_container bash -c 'echo $PATH'





    share|improve this answer


















    • 1





      Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

      – dave_thompson_085
      May 10 at 3:48














    5












    5








    5







    In your first example:



    docker exec -i -t my_container bash -c "echo $PATH"


    That will evaluate the $PATH variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH on the command line outside of docker and see that they are the same.



    In your second example:



    docker exec -i -t my_container bash 
    root@21e6d898c3c2:/# echo $PATH


    That will evaluate the $PATH variable inside the container.



    You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:



    docker exec -i -t my_container bash -c "echo $PATH"
    docker exec -i -t my_container bash -c 'echo $PATH'





    share|improve this answer













    In your first example:



    docker exec -i -t my_container bash -c "echo $PATH"


    That will evaluate the $PATH variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH on the command line outside of docker and see that they are the same.



    In your second example:



    docker exec -i -t my_container bash 
    root@21e6d898c3c2:/# echo $PATH


    That will evaluate the $PATH variable inside the container.



    You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:



    docker exec -i -t my_container bash -c "echo $PATH"
    docker exec -i -t my_container bash -c 'echo $PATH'






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 27 '17 at 21:22









    BMitchBMitch

    1,685714




    1,685714







    • 1





      Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

      – dave_thompson_085
      May 10 at 3:48













    • 1





      Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

      – dave_thompson_085
      May 10 at 3:48








    1




    1





    Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

    – dave_thompson_085
    May 10 at 3:48






    Or ... bash -c "printenv PATH" or ... bash -c "declare -p PATH" which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup

    – dave_thompson_085
    May 10 at 3:48












    1














    Try the -l option for bash. It will run in login shell and load /etc/profile.



    docker exec -i -t my_container bash -lc "echo $PATH"





    share|improve this answer





























      1














      Try the -l option for bash. It will run in login shell and load /etc/profile.



      docker exec -i -t my_container bash -lc "echo $PATH"





      share|improve this answer



























        1












        1








        1







        Try the -l option for bash. It will run in login shell and load /etc/profile.



        docker exec -i -t my_container bash -lc "echo $PATH"





        share|improve this answer















        Try the -l option for bash. It will run in login shell and load /etc/profile.



        docker exec -i -t my_container bash -lc "echo $PATH"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 10 at 3:00









        chicks

        3,08572033




        3,08572033










        answered Nov 22 '17 at 21:23









        Max IvakMax Ivak

        1113




        1113



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Server Fault!


            • 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%2fserverfault.com%2fquestions%2f816709%2fwhy-are-environment-variables-different-with-bash-c%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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company