Replace environment variables in text if they exist

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















I know that envsubst replaces declared environment variables in the input.



$ echo 'Hello $USER' | envsubst
Hello myusername


What I want is a way to replace the environment variable if it exists otherwise envsusbst (or any other command), leaves the variable string as it is. What I get is:



$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
Hello myusername


What I want is:



$ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
Hello myusername $UNDEFINED_VARIABLE









share|improve this question


























    2















    I know that envsubst replaces declared environment variables in the input.



    $ echo 'Hello $USER' | envsubst
    Hello myusername


    What I want is a way to replace the environment variable if it exists otherwise envsusbst (or any other command), leaves the variable string as it is. What I get is:



    $ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
    Hello myusername


    What I want is:



    $ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
    Hello myusername $UNDEFINED_VARIABLE









    share|improve this question
























      2












      2








      2








      I know that envsubst replaces declared environment variables in the input.



      $ echo 'Hello $USER' | envsubst
      Hello myusername


      What I want is a way to replace the environment variable if it exists otherwise envsusbst (or any other command), leaves the variable string as it is. What I get is:



      $ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
      Hello myusername


      What I want is:



      $ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
      Hello myusername $UNDEFINED_VARIABLE









      share|improve this question














      I know that envsubst replaces declared environment variables in the input.



      $ echo 'Hello $USER' | envsubst
      Hello myusername


      What I want is a way to replace the environment variable if it exists otherwise envsusbst (or any other command), leaves the variable string as it is. What I get is:



      $ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
      Hello myusername


      What I want is:



      $ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
      Hello myusername $UNDEFINED_VARIABLE






      linux bash shell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 6 at 8:49









      Farzad VertigoFarzad Vertigo

      1133




      1133




















          2 Answers
          2






          active

          oldest

          votes


















          1














          It you pass an argument like $USER$PATH to envsubst, then it expands only those variables that are referenced in that argument.



          So one way could be to pass it all the currently defined environment variables in that format. With zsh:



          echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
          envsubst $$(kj:$:)parameters[(R)*export*]



          • $parameters is a special associative array that maps variable names to their type


          • $parameters[(R)*export*] expands to all the elements of the associative array whose value contains export.

          • with the k parameter expansion flag, the key instead of the value is returned


          • j:$: joins those elements with $ in between, and we add one at the start.

          With other shells, you can always revert to perl to get that list:



          echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
          envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"


          Beware both disclose your environment variable names in the output of ps.



          Instead, you could also do the whole thing in perl:



          perl -pe 's(?$ENV$1//$&ge'


          Beware it has the same limitations as envsubst in that it won't expand things like $VAR:-x and would expand $HOME in things like $HOME or $$HOME which a shell wouldn't.






          share|improve this answer
































            2














            You can use env to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)



            echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"


            (The output of env lists the values of the variables as well, but envsubst also wants to see a leading $, so we can't just use cut -d= -f1 on its own, unfortunately. You could use a single sed to do cut's job as well, see previous revision, but I prefer the clarity of cut over a tiny performance gain.)






            share|improve this answer























            • That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

              – Stéphane Chazelas
              Jan 6 at 9:17











            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%2f492772%2freplace-environment-variables-in-text-if-they-exist%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            It you pass an argument like $USER$PATH to envsubst, then it expands only those variables that are referenced in that argument.



            So one way could be to pass it all the currently defined environment variables in that format. With zsh:



            echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
            envsubst $$(kj:$:)parameters[(R)*export*]



            • $parameters is a special associative array that maps variable names to their type


            • $parameters[(R)*export*] expands to all the elements of the associative array whose value contains export.

            • with the k parameter expansion flag, the key instead of the value is returned


            • j:$: joins those elements with $ in between, and we add one at the start.

            With other shells, you can always revert to perl to get that list:



            echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
            envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"


            Beware both disclose your environment variable names in the output of ps.



            Instead, you could also do the whole thing in perl:



            perl -pe 's(?$ENV$1//$&ge'


            Beware it has the same limitations as envsubst in that it won't expand things like $VAR:-x and would expand $HOME in things like $HOME or $$HOME which a shell wouldn't.






            share|improve this answer





























              1














              It you pass an argument like $USER$PATH to envsubst, then it expands only those variables that are referenced in that argument.



              So one way could be to pass it all the currently defined environment variables in that format. With zsh:



              echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
              envsubst $$(kj:$:)parameters[(R)*export*]



              • $parameters is a special associative array that maps variable names to their type


              • $parameters[(R)*export*] expands to all the elements of the associative array whose value contains export.

              • with the k parameter expansion flag, the key instead of the value is returned


              • j:$: joins those elements with $ in between, and we add one at the start.

              With other shells, you can always revert to perl to get that list:



              echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
              envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"


              Beware both disclose your environment variable names in the output of ps.



              Instead, you could also do the whole thing in perl:



              perl -pe 's(?$ENV$1//$&ge'


              Beware it has the same limitations as envsubst in that it won't expand things like $VAR:-x and would expand $HOME in things like $HOME or $$HOME which a shell wouldn't.






              share|improve this answer



























                1












                1








                1







                It you pass an argument like $USER$PATH to envsubst, then it expands only those variables that are referenced in that argument.



                So one way could be to pass it all the currently defined environment variables in that format. With zsh:



                echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
                envsubst $$(kj:$:)parameters[(R)*export*]



                • $parameters is a special associative array that maps variable names to their type


                • $parameters[(R)*export*] expands to all the elements of the associative array whose value contains export.

                • with the k parameter expansion flag, the key instead of the value is returned


                • j:$: joins those elements with $ in between, and we add one at the start.

                With other shells, you can always revert to perl to get that list:



                echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
                envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"


                Beware both disclose your environment variable names in the output of ps.



                Instead, you could also do the whole thing in perl:



                perl -pe 's(?$ENV$1//$&ge'


                Beware it has the same limitations as envsubst in that it won't expand things like $VAR:-x and would expand $HOME in things like $HOME or $$HOME which a shell wouldn't.






                share|improve this answer















                It you pass an argument like $USER$PATH to envsubst, then it expands only those variables that are referenced in that argument.



                So one way could be to pass it all the currently defined environment variables in that format. With zsh:



                echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
                envsubst $$(kj:$:)parameters[(R)*export*]



                • $parameters is a special associative array that maps variable names to their type


                • $parameters[(R)*export*] expands to all the elements of the associative array whose value contains export.

                • with the k parameter expansion flag, the key instead of the value is returned


                • j:$: joins those elements with $ in between, and we add one at the start.

                With other shells, you can always revert to perl to get that list:



                echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
                envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"


                Beware both disclose your environment variable names in the output of ps.



                Instead, you could also do the whole thing in perl:



                perl -pe 's(?$ENV$1//$&ge'


                Beware it has the same limitations as envsubst in that it won't expand things like $VAR:-x and would expand $HOME in things like $HOME or $$HOME which a shell wouldn't.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 6 at 9:41

























                answered Jan 6 at 9:11









                Stéphane ChazelasStéphane Chazelas

                301k55565917




                301k55565917























                    2














                    You can use env to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)



                    echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"


                    (The output of env lists the values of the variables as well, but envsubst also wants to see a leading $, so we can't just use cut -d= -f1 on its own, unfortunately. You could use a single sed to do cut's job as well, see previous revision, but I prefer the clarity of cut over a tiny performance gain.)






                    share|improve this answer























                    • That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                      – Stéphane Chazelas
                      Jan 6 at 9:17
















                    2














                    You can use env to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)



                    echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"


                    (The output of env lists the values of the variables as well, but envsubst also wants to see a leading $, so we can't just use cut -d= -f1 on its own, unfortunately. You could use a single sed to do cut's job as well, see previous revision, but I prefer the clarity of cut over a tiny performance gain.)






                    share|improve this answer























                    • That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                      – Stéphane Chazelas
                      Jan 6 at 9:17














                    2












                    2








                    2







                    You can use env to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)



                    echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"


                    (The output of env lists the values of the variables as well, but envsubst also wants to see a leading $, so we can't just use cut -d= -f1 on its own, unfortunately. You could use a single sed to do cut's job as well, see previous revision, but I prefer the clarity of cut over a tiny performance gain.)






                    share|improve this answer













                    You can use env to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)



                    echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"


                    (The output of env lists the values of the variables as well, but envsubst also wants to see a leading $, so we can't just use cut -d= -f1 on its own, unfortunately. You could use a single sed to do cut's job as well, see previous revision, but I prefer the clarity of cut over a tiny performance gain.)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 6 at 9:08









                    Ulrich SchwarzUlrich Schwarz

                    9,62512946




                    9,62512946












                    • That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                      – Stéphane Chazelas
                      Jan 6 at 9:17


















                    • That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                      – Stéphane Chazelas
                      Jan 6 at 9:17

















                    That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                    – Stéphane Chazelas
                    Jan 6 at 9:17






                    That assumes none of the env variables names or values contain newline characters (and names don't contain $ characters)

                    – Stéphane Chazelas
                    Jan 6 at 9:17


















                    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%2f492772%2freplace-environment-variables-in-text-if-they-exist%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

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay