How do I use variables in single quoted strings?












17














I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).



     echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}


any help would be greatly appreciated










share|improve this question
























  • See also stackoverflow.com/questions/10067266/…
    – tripleee
    Jul 15 '18 at 19:06










  • See Difference between single and double quotes in Bash as well.
    – codeforester
    Sep 13 '18 at 21:53
















17














I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).



     echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}


any help would be greatly appreciated










share|improve this question
























  • See also stackoverflow.com/questions/10067266/…
    – tripleee
    Jul 15 '18 at 19:06










  • See Difference between single and double quotes in Bash as well.
    – codeforester
    Sep 13 '18 at 21:53














17












17








17


1





I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).



     echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}


any help would be greatly appreciated










share|improve this question















I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).



     echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}


any help would be greatly appreciated







string bash shell echo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 30 '18 at 23:16









that other guy

71.8k885123




71.8k885123










asked Jan 17 '14 at 17:44









Pectus Excavatum

1,14892652




1,14892652












  • See also stackoverflow.com/questions/10067266/…
    – tripleee
    Jul 15 '18 at 19:06










  • See Difference between single and double quotes in Bash as well.
    – codeforester
    Sep 13 '18 at 21:53


















  • See also stackoverflow.com/questions/10067266/…
    – tripleee
    Jul 15 '18 at 19:06










  • See Difference between single and double quotes in Bash as well.
    – codeforester
    Sep 13 '18 at 21:53
















See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06




See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06












See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53




See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53












7 Answers
7






active

oldest

votes


















24














Variables are expanded in double quoted strings, but not in single quoted strings:



 $ name=World

$ echo "Hello $name"
Hello World

$ echo 'Hello $name'
Hello $name


If you can simply switch quotes, do so.



If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:



 $ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.

$ echo '"$name" has the value '"$name"
"$name" has the value World


Applied to your case:



 echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"





share|improve this answer



















  • 2




    Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
    – twalberg
    Jan 17 '14 at 18:03








  • 5




    Don't forget that you have to quote "$FILE".
    – Aleks-Daniel Jakimenko-A.
    Jan 18 '14 at 4:15










  • @Aleks-DanielJakimenko-A. Is it necessary?
    – Josh Detwiler
    Jul 2 '18 at 20:24






  • 1




    @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
    – Aleks-Daniel Jakimenko-A.
    Jul 3 '18 at 21:02



















6














use printf:



printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}





share|improve this answer

















  • 6




    Please quote "$FILE".
    – Aleks-Daniel Jakimenko-A.
    Jan 18 '14 at 4:14



















4














Use a heredoc:



cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF





share|improve this answer





























    2














    The most readable, functional way uses curly braces inside double quotes.



    'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"





    share|improve this answer

















    • 1




      Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
      – Jonas Berlin
      Oct 14 '16 at 22:00










    • @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
      – mklement0
      Oct 14 '16 at 22:06






    • 1




      Unfortunately I do not have the reputation to leave a comment.
      – Paul Back
      Oct 17 '16 at 17:55



















    0














    with a subshell:



    var='hello' echo 'blah_'`echo $var`' blah blah';





    share|improve this answer



















    • 1




      Does not work, echoes blah_`echo $var` blah blah
      – Jonas Berlin
      Oct 14 '16 at 21:57












    • You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
      – R.Sicart
      Oct 18 '16 at 14:40










    • Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
      – Jonas Berlin
      Oct 19 '16 at 19:04










    • That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
      – tripleee
      Jul 2 '18 at 20:17





















    0














    You can do it this way:



    $ counter=1 eval echo `echo 'test text 
    "here_is_some_test_text_$counter" "output"' |
    sed -s 's/"/\\"/g'` > file

    cat file
    test text "here_is_some_test_text_1" "output"


    Explanation:
    Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.



    It says execute the following string as command:



    'echo test text "here_is_some_test_text_$counter" "output"'


    Command again in one line:



    counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file





    share|improve this answer





























      0














      Output a variable wrapped with single quotes:



      printf "'"'Hello %s'"'" world





      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%2f21192420%2fhow-do-i-use-variables-in-single-quoted-strings%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        24














        Variables are expanded in double quoted strings, but not in single quoted strings:



         $ name=World

        $ echo "Hello $name"
        Hello World

        $ echo 'Hello $name'
        Hello $name


        If you can simply switch quotes, do so.



        If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:



         $ echo 'single quoted. '"Double quoted. "'Single quoted again.'
        single quoted. Double quoted. Single quoted again.

        $ echo '"$name" has the value '"$name"
        "$name" has the value World


        Applied to your case:



         echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"





        share|improve this answer



















        • 2




          Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
          – twalberg
          Jan 17 '14 at 18:03








        • 5




          Don't forget that you have to quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:15










        • @Aleks-DanielJakimenko-A. Is it necessary?
          – Josh Detwiler
          Jul 2 '18 at 20:24






        • 1




          @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
          – Aleks-Daniel Jakimenko-A.
          Jul 3 '18 at 21:02
















        24














        Variables are expanded in double quoted strings, but not in single quoted strings:



         $ name=World

        $ echo "Hello $name"
        Hello World

        $ echo 'Hello $name'
        Hello $name


        If you can simply switch quotes, do so.



        If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:



         $ echo 'single quoted. '"Double quoted. "'Single quoted again.'
        single quoted. Double quoted. Single quoted again.

        $ echo '"$name" has the value '"$name"
        "$name" has the value World


        Applied to your case:



         echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"





        share|improve this answer



















        • 2




          Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
          – twalberg
          Jan 17 '14 at 18:03








        • 5




          Don't forget that you have to quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:15










        • @Aleks-DanielJakimenko-A. Is it necessary?
          – Josh Detwiler
          Jul 2 '18 at 20:24






        • 1




          @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
          – Aleks-Daniel Jakimenko-A.
          Jul 3 '18 at 21:02














        24












        24








        24






        Variables are expanded in double quoted strings, but not in single quoted strings:



         $ name=World

        $ echo "Hello $name"
        Hello World

        $ echo 'Hello $name'
        Hello $name


        If you can simply switch quotes, do so.



        If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:



         $ echo 'single quoted. '"Double quoted. "'Single quoted again.'
        single quoted. Double quoted. Single quoted again.

        $ echo '"$name" has the value '"$name"
        "$name" has the value World


        Applied to your case:



         echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"





        share|improve this answer














        Variables are expanded in double quoted strings, but not in single quoted strings:



         $ name=World

        $ echo "Hello $name"
        Hello World

        $ echo 'Hello $name'
        Hello $name


        If you can simply switch quotes, do so.



        If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:



         $ echo 'single quoted. '"Double quoted. "'Single quoted again.'
        single quoted. Double quoted. Single quoted again.

        $ echo '"$name" has the value '"$name"
        "$name" has the value World


        Applied to your case:



         echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 3 '18 at 21:02









        Aleks-Daniel Jakimenko-A.

        7,5652931




        7,5652931










        answered Jan 17 '14 at 17:45









        Ignacio Vazquez-Abrams

        577k10010561158




        577k10010561158








        • 2




          Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
          – twalberg
          Jan 17 '14 at 18:03








        • 5




          Don't forget that you have to quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:15










        • @Aleks-DanielJakimenko-A. Is it necessary?
          – Josh Detwiler
          Jul 2 '18 at 20:24






        • 1




          @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
          – Aleks-Daniel Jakimenko-A.
          Jul 3 '18 at 21:02














        • 2




          Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
          – twalberg
          Jan 17 '14 at 18:03








        • 5




          Don't forget that you have to quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:15










        • @Aleks-DanielJakimenko-A. Is it necessary?
          – Josh Detwiler
          Jul 2 '18 at 20:24






        • 1




          @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
          – Aleks-Daniel Jakimenko-A.
          Jul 3 '18 at 21:02








        2




        2




        Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
        – twalberg
        Jan 17 '14 at 18:03






        Alternatively, echo "test text "here_is_some_test_text_$counter" "output""... Escape the double quotes you don't want the shell to interpret.
        – twalberg
        Jan 17 '14 at 18:03






        5




        5




        Don't forget that you have to quote "$FILE".
        – Aleks-Daniel Jakimenko-A.
        Jan 18 '14 at 4:15




        Don't forget that you have to quote "$FILE".
        – Aleks-Daniel Jakimenko-A.
        Jan 18 '14 at 4:15












        @Aleks-DanielJakimenko-A. Is it necessary?
        – Josh Detwiler
        Jul 2 '18 at 20:24




        @Aleks-DanielJakimenko-A. Is it necessary?
        – Josh Detwiler
        Jul 2 '18 at 20:24




        1




        1




        @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
        – Aleks-Daniel Jakimenko-A.
        Jul 3 '18 at 21:02




        @JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
        – Aleks-Daniel Jakimenko-A.
        Jul 3 '18 at 21:02













        6














        use printf:



        printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}





        share|improve this answer

















        • 6




          Please quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:14
















        6














        use printf:



        printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}





        share|improve this answer

















        • 6




          Please quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:14














        6












        6








        6






        use printf:



        printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}





        share|improve this answer












        use printf:



        printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> ${FILE}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 17 '14 at 18:07









        glenn jackman

        166k26143234




        166k26143234








        • 6




          Please quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:14














        • 6




          Please quote "$FILE".
          – Aleks-Daniel Jakimenko-A.
          Jan 18 '14 at 4:14








        6




        6




        Please quote "$FILE".
        – Aleks-Daniel Jakimenko-A.
        Jan 18 '14 at 4:14




        Please quote "$FILE".
        – Aleks-Daniel Jakimenko-A.
        Jan 18 '14 at 4:14











        4














        Use a heredoc:



        cat << EOF >> ${FILE}
        test text "here_is_some_test_text_$counter" "output"
        EOF





        share|improve this answer


























          4














          Use a heredoc:



          cat << EOF >> ${FILE}
          test text "here_is_some_test_text_$counter" "output"
          EOF





          share|improve this answer
























            4












            4








            4






            Use a heredoc:



            cat << EOF >> ${FILE}
            test text "here_is_some_test_text_$counter" "output"
            EOF





            share|improve this answer












            Use a heredoc:



            cat << EOF >> ${FILE}
            test text "here_is_some_test_text_$counter" "output"
            EOF






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 17 '14 at 18:55









            William Pursell

            130k32206236




            130k32206236























                2














                The most readable, functional way uses curly braces inside double quotes.



                'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"





                share|improve this answer

















                • 1




                  Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                  – Jonas Berlin
                  Oct 14 '16 at 22:00










                • @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                  – mklement0
                  Oct 14 '16 at 22:06






                • 1




                  Unfortunately I do not have the reputation to leave a comment.
                  – Paul Back
                  Oct 17 '16 at 17:55
















                2














                The most readable, functional way uses curly braces inside double quotes.



                'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"





                share|improve this answer

















                • 1




                  Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                  – Jonas Berlin
                  Oct 14 '16 at 22:00










                • @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                  – mklement0
                  Oct 14 '16 at 22:06






                • 1




                  Unfortunately I do not have the reputation to leave a comment.
                  – Paul Back
                  Oct 17 '16 at 17:55














                2












                2








                2






                The most readable, functional way uses curly braces inside double quotes.



                'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"





                share|improve this answer












                The most readable, functional way uses curly braces inside double quotes.



                'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 14 '16 at 21:54









                Paul Back

                753915




                753915








                • 1




                  Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                  – Jonas Berlin
                  Oct 14 '16 at 22:00










                • @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                  – mklement0
                  Oct 14 '16 at 22:06






                • 1




                  Unfortunately I do not have the reputation to leave a comment.
                  – Paul Back
                  Oct 17 '16 at 17:55














                • 1




                  Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                  – Jonas Berlin
                  Oct 14 '16 at 22:00










                • @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                  – mklement0
                  Oct 14 '16 at 22:06






                • 1




                  Unfortunately I do not have the reputation to leave a comment.
                  – Paul Back
                  Oct 17 '16 at 17:55








                1




                1




                Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                – Jonas Berlin
                Oct 14 '16 at 22:00




                Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
                – Jonas Berlin
                Oct 14 '16 at 22:00












                @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                – mklement0
                Oct 14 '16 at 22:06




                @JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
                – mklement0
                Oct 14 '16 at 22:06




                1




                1




                Unfortunately I do not have the reputation to leave a comment.
                – Paul Back
                Oct 17 '16 at 17:55




                Unfortunately I do not have the reputation to leave a comment.
                – Paul Back
                Oct 17 '16 at 17:55











                0














                with a subshell:



                var='hello' echo 'blah_'`echo $var`' blah blah';





                share|improve this answer



















                • 1




                  Does not work, echoes blah_`echo $var` blah blah
                  – Jonas Berlin
                  Oct 14 '16 at 21:57












                • You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                  – R.Sicart
                  Oct 18 '16 at 14:40










                • Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                  – Jonas Berlin
                  Oct 19 '16 at 19:04










                • That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                  – tripleee
                  Jul 2 '18 at 20:17


















                0














                with a subshell:



                var='hello' echo 'blah_'`echo $var`' blah blah';





                share|improve this answer



















                • 1




                  Does not work, echoes blah_`echo $var` blah blah
                  – Jonas Berlin
                  Oct 14 '16 at 21:57












                • You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                  – R.Sicart
                  Oct 18 '16 at 14:40










                • Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                  – Jonas Berlin
                  Oct 19 '16 at 19:04










                • That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                  – tripleee
                  Jul 2 '18 at 20:17
















                0












                0








                0






                with a subshell:



                var='hello' echo 'blah_'`echo $var`' blah blah';





                share|improve this answer














                with a subshell:



                var='hello' echo 'blah_'`echo $var`' blah blah';






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 18 '16 at 14:40

























                answered Jan 17 '14 at 18:32









                R.Sicart

                56029




                56029








                • 1




                  Does not work, echoes blah_`echo $var` blah blah
                  – Jonas Berlin
                  Oct 14 '16 at 21:57












                • You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                  – R.Sicart
                  Oct 18 '16 at 14:40










                • Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                  – Jonas Berlin
                  Oct 19 '16 at 19:04










                • That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                  – tripleee
                  Jul 2 '18 at 20:17
















                • 1




                  Does not work, echoes blah_`echo $var` blah blah
                  – Jonas Berlin
                  Oct 14 '16 at 21:57












                • You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                  – R.Sicart
                  Oct 18 '16 at 14:40










                • Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                  – Jonas Berlin
                  Oct 19 '16 at 19:04










                • That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                  – tripleee
                  Jul 2 '18 at 20:17










                1




                1




                Does not work, echoes blah_`echo $var` blah blah
                – Jonas Berlin
                Oct 14 '16 at 21:57






                Does not work, echoes blah_`echo $var` blah blah
                – Jonas Berlin
                Oct 14 '16 at 21:57














                You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                – R.Sicart
                Oct 18 '16 at 14:40




                You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
                – R.Sicart
                Oct 18 '16 at 14:40












                Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                – Jonas Berlin
                Oct 19 '16 at 19:04




                Your new answer will compress any whitespace in $var.. please see Ignacio Vazquez-Abrams' answer..
                – Jonas Berlin
                Oct 19 '16 at 19:04












                That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                – tripleee
                Jul 2 '18 at 20:17






                That's a useless use of echo. You'll be fine with 'blah_'"$var"' blah blah.' But that's already in Ignacio's answer.
                – tripleee
                Jul 2 '18 at 20:17













                0














                You can do it this way:



                $ counter=1 eval echo `echo 'test text 
                "here_is_some_test_text_$counter" "output"' |
                sed -s 's/"/\\"/g'` > file

                cat file
                test text "here_is_some_test_text_1" "output"


                Explanation:
                Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.



                It says execute the following string as command:



                'echo test text "here_is_some_test_text_$counter" "output"'


                Command again in one line:



                counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file





                share|improve this answer


























                  0














                  You can do it this way:



                  $ counter=1 eval echo `echo 'test text 
                  "here_is_some_test_text_$counter" "output"' |
                  sed -s 's/"/\\"/g'` > file

                  cat file
                  test text "here_is_some_test_text_1" "output"


                  Explanation:
                  Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.



                  It says execute the following string as command:



                  'echo test text "here_is_some_test_text_$counter" "output"'


                  Command again in one line:



                  counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file





                  share|improve this answer
























                    0












                    0








                    0






                    You can do it this way:



                    $ counter=1 eval echo `echo 'test text 
                    "here_is_some_test_text_$counter" "output"' |
                    sed -s 's/"/\\"/g'` > file

                    cat file
                    test text "here_is_some_test_text_1" "output"


                    Explanation:
                    Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.



                    It says execute the following string as command:



                    'echo test text "here_is_some_test_text_$counter" "output"'


                    Command again in one line:



                    counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file





                    share|improve this answer












                    You can do it this way:



                    $ counter=1 eval echo `echo 'test text 
                    "here_is_some_test_text_$counter" "output"' |
                    sed -s 's/"/\\"/g'` > file

                    cat file
                    test text "here_is_some_test_text_1" "output"


                    Explanation:
                    Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.



                    It says execute the following string as command:



                    'echo test text "here_is_some_test_text_$counter" "output"'


                    Command again in one line:



                    counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 20 '17 at 15:17









                    Kulimak Joco

                    91




                    91























                        0














                        Output a variable wrapped with single quotes:



                        printf "'"'Hello %s'"'" world





                        share|improve this answer


























                          0














                          Output a variable wrapped with single quotes:



                          printf "'"'Hello %s'"'" world





                          share|improve this answer
























                            0












                            0








                            0






                            Output a variable wrapped with single quotes:



                            printf "'"'Hello %s'"'" world





                            share|improve this answer












                            Output a variable wrapped with single quotes:



                            printf "'"'Hello %s'"'" world






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 3 '17 at 9:07









                            Elior Malul

                            30315




                            30315






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f21192420%2fhow-do-i-use-variables-in-single-quoted-strings%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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas