Unable to assign env variable using a command's output












1















I can set environment variables like this.



➤ foo=bar env | grep foo
foo=bar


But, what if, I can only get foo=bar string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo command.



➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar


In this, zsh/bash starts interpreting it as a command. How do I fix this?










share|improve this question


















  • 1





    It's not clear what you are asking.

    – Emily E.
    Nov 22 '18 at 13:24






  • 1





    FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

    – pjh
    Nov 22 '18 at 19:18
















1















I can set environment variables like this.



➤ foo=bar env | grep foo
foo=bar


But, what if, I can only get foo=bar string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo command.



➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar


In this, zsh/bash starts interpreting it as a command. How do I fix this?










share|improve this question


















  • 1





    It's not clear what you are asking.

    – Emily E.
    Nov 22 '18 at 13:24






  • 1





    FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

    – pjh
    Nov 22 '18 at 19:18














1












1








1


1






I can set environment variables like this.



➤ foo=bar env | grep foo
foo=bar


But, what if, I can only get foo=bar string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo command.



➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar


In this, zsh/bash starts interpreting it as a command. How do I fix this?










share|improve this question














I can set environment variables like this.



➤ foo=bar env | grep foo
foo=bar


But, what if, I can only get foo=bar string after I execute a command (which is my use-case). The simplest way to emulate this is using a simple echo command.



➤ `echo foo=bar` env | grep foo
zsh: command not found: foo=bar


In this, zsh/bash starts interpreting it as a command. How do I fix this?







bash zsh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:50









Abhijeet RastogiAbhijeet Rastogi

10.1k1965108




10.1k1965108








  • 1





    It's not clear what you are asking.

    – Emily E.
    Nov 22 '18 at 13:24






  • 1





    FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

    – pjh
    Nov 22 '18 at 19:18














  • 1





    It's not clear what you are asking.

    – Emily E.
    Nov 22 '18 at 13:24






  • 1





    FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

    – pjh
    Nov 22 '18 at 19:18








1




1





It's not clear what you are asking.

– Emily E.
Nov 22 '18 at 13:24





It's not clear what you are asking.

– Emily E.
Nov 22 '18 at 13:24




1




1





FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

– pjh
Nov 22 '18 at 19:18





FWIW, I find this question very clear. I think it's a good example of How to create a Minimal, Complete, and Verifiable example (MCVE).

– pjh
Nov 22 '18 at 19:18












3 Answers
3






active

oldest

votes


















3














The problem is that the shell looks for the form var1=val1 var2=val2 ... command before doing expansions, including command substitution (`...` or $(...)).



One way to work around the problem is to use eval to cause the shell to do its parsing after the command substitution is done:



eval "$(echo foo=bar)" env | grep foo


(See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...` to $(...).)



Unfortunately, eval is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.



Another alternative is to use export in a subshell. One way is:



{ export "$(echo foo=bar)" ; env ; } | grep foo


Since the export is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.



( export "$(echo foo=bar)" ; env ) > tmpfile
grep foo tmpfile





share|improve this answer































    0














    It is working for me as expected. Have you tried like below?



    env `echo foo=bar` | grep foo





    share|improve this answer































      0














      I'm not sure if I understood your question, but try this:



      $ echo "hello" && foo=bar env | grep foo
      hello
      foo=bar





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        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%2fstackoverflow.com%2fquestions%2f53431446%2funable-to-assign-env-variable-using-a-commands-output%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









        3














        The problem is that the shell looks for the form var1=val1 var2=val2 ... command before doing expansions, including command substitution (`...` or $(...)).



        One way to work around the problem is to use eval to cause the shell to do its parsing after the command substitution is done:



        eval "$(echo foo=bar)" env | grep foo


        (See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...` to $(...).)



        Unfortunately, eval is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.



        Another alternative is to use export in a subshell. One way is:



        { export "$(echo foo=bar)" ; env ; } | grep foo


        Since the export is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.



        ( export "$(echo foo=bar)" ; env ) > tmpfile
        grep foo tmpfile





        share|improve this answer




























          3














          The problem is that the shell looks for the form var1=val1 var2=val2 ... command before doing expansions, including command substitution (`...` or $(...)).



          One way to work around the problem is to use eval to cause the shell to do its parsing after the command substitution is done:



          eval "$(echo foo=bar)" env | grep foo


          (See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...` to $(...).)



          Unfortunately, eval is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.



          Another alternative is to use export in a subshell. One way is:



          { export "$(echo foo=bar)" ; env ; } | grep foo


          Since the export is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.



          ( export "$(echo foo=bar)" ; env ) > tmpfile
          grep foo tmpfile





          share|improve this answer


























            3












            3








            3







            The problem is that the shell looks for the form var1=val1 var2=val2 ... command before doing expansions, including command substitution (`...` or $(...)).



            One way to work around the problem is to use eval to cause the shell to do its parsing after the command substitution is done:



            eval "$(echo foo=bar)" env | grep foo


            (See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...` to $(...).)



            Unfortunately, eval is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.



            Another alternative is to use export in a subshell. One way is:



            { export "$(echo foo=bar)" ; env ; } | grep foo


            Since the export is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.



            ( export "$(echo foo=bar)" ; env ) > tmpfile
            grep foo tmpfile





            share|improve this answer













            The problem is that the shell looks for the form var1=val1 var2=val2 ... command before doing expansions, including command substitution (`...` or $(...)).



            One way to work around the problem is to use eval to cause the shell to do its parsing after the command substitution is done:



            eval "$(echo foo=bar)" env | grep foo


            (See What is the benefit of using $() instead of backticks in shell scripts? for an explanation of why I've changed `...` to $(...).)



            Unfortunately, eval is potentially dangerous. It should not be used unless there is no other alternative. See Why should eval be avoided in Bash, and what should I use instead?.



            Another alternative is to use export in a subshell. One way is:



            { export "$(echo foo=bar)" ; env ; } | grep foo


            Since the export is done in a pipeline (and not the last part of a pipeline, which would make a difference for some shells or some modes) it doesn't affect the variable settings in the current shell. If the command is not in a pipeline, then it would be necessary to explicitly run it in a subshell to get the same effect. E.g.



            ( export "$(echo foo=bar)" ; env ) > tmpfile
            grep foo tmpfile






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 19:43









            pjhpjh

            1,789612




            1,789612

























                0














                It is working for me as expected. Have you tried like below?



                env `echo foo=bar` | grep foo





                share|improve this answer




























                  0














                  It is working for me as expected. Have you tried like below?



                  env `echo foo=bar` | grep foo





                  share|improve this answer


























                    0












                    0








                    0







                    It is working for me as expected. Have you tried like below?



                    env `echo foo=bar` | grep foo





                    share|improve this answer













                    It is working for me as expected. Have you tried like below?



                    env `echo foo=bar` | grep foo






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 13:02









                    Mohit KumarMohit Kumar

                    706521




                    706521























                        0














                        I'm not sure if I understood your question, but try this:



                        $ echo "hello" && foo=bar env | grep foo
                        hello
                        foo=bar





                        share|improve this answer




























                          0














                          I'm not sure if I understood your question, but try this:



                          $ echo "hello" && foo=bar env | grep foo
                          hello
                          foo=bar





                          share|improve this answer


























                            0












                            0








                            0







                            I'm not sure if I understood your question, but try this:



                            $ echo "hello" && foo=bar env | grep foo
                            hello
                            foo=bar





                            share|improve this answer













                            I'm not sure if I understood your question, but try this:



                            $ echo "hello" && foo=bar env | grep foo
                            hello
                            foo=bar






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 13:03









                            downtheroaddowntheroad

                            1206




                            1206






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f53431446%2funable-to-assign-env-variable-using-a-commands-output%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

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory