Bash Command Substitution doesn't work












-1














This is the script:



#!/bin/bash

thedate=$(date)


var='Current date is $thedate'

echo $var


The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.










share|improve this question



























    -1














    This is the script:



    #!/bin/bash

    thedate=$(date)


    var='Current date is $thedate'

    echo $var


    The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.










    share|improve this question

























      -1












      -1








      -1







      This is the script:



      #!/bin/bash

      thedate=$(date)


      var='Current date is $thedate'

      echo $var


      The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.










      share|improve this question













      This is the script:



      #!/bin/bash

      thedate=$(date)


      var='Current date is $thedate'

      echo $var


      The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.







      bash script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 7 at 18:36









      Iulia Danilov

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:



          eval echo $var


          Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-



          var='Current date is $(date)'
          eval echo $var


          It would be better not to use variables, but define a function:-



          cdate() { echo Current date is $(date); }
          ...
          cdate


          Of course, it is better still not to use echo, but let date itself add the extra text:



          cdate() { date +"Current date is %c"; }


          This doesn't output quite the same format as the date default, but there is no format specifier for the default.






          share|improve this answer





























            0














            var='Current date is $thedate'


            Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.



            echo $var


            Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.






            share|improve this answer

















            • 1




              If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
              – AFH
              Dec 7 at 18:45












            • @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
              – ilkkachu
              Dec 7 at 18:50










            • Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
              – AFH
              Dec 7 at 18:55











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f1381718%2fbash-command-substitution-doesnt-work%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














            The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:



            eval echo $var


            Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-



            var='Current date is $(date)'
            eval echo $var


            It would be better not to use variables, but define a function:-



            cdate() { echo Current date is $(date); }
            ...
            cdate


            Of course, it is better still not to use echo, but let date itself add the extra text:



            cdate() { date +"Current date is %c"; }


            This doesn't output quite the same format as the date default, but there is no format specifier for the default.






            share|improve this answer


























              1














              The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:



              eval echo $var


              Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-



              var='Current date is $(date)'
              eval echo $var


              It would be better not to use variables, but define a function:-



              cdate() { echo Current date is $(date); }
              ...
              cdate


              Of course, it is better still not to use echo, but let date itself add the extra text:



              cdate() { date +"Current date is %c"; }


              This doesn't output quite the same format as the date default, but there is no format specifier for the default.






              share|improve this answer
























                1












                1








                1






                The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:



                eval echo $var


                Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-



                var='Current date is $(date)'
                eval echo $var


                It would be better not to use variables, but define a function:-



                cdate() { echo Current date is $(date); }
                ...
                cdate


                Of course, it is better still not to use echo, but let date itself add the extra text:



                cdate() { date +"Current date is %c"; }


                This doesn't output quite the same format as the date default, but there is no format specifier for the default.






                share|improve this answer












                The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:



                eval echo $var


                Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-



                var='Current date is $(date)'
                eval echo $var


                It would be better not to use variables, but define a function:-



                cdate() { echo Current date is $(date); }
                ...
                cdate


                Of course, it is better still not to use echo, but let date itself add the extra text:



                cdate() { date +"Current date is %c"; }


                This doesn't output quite the same format as the date default, but there is no format specifier for the default.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 7 at 19:38









                AFH

                13.9k31938




                13.9k31938

























                    0














                    var='Current date is $thedate'


                    Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.



                    echo $var


                    Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.






                    share|improve this answer

















                    • 1




                      If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                      – AFH
                      Dec 7 at 18:45












                    • @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                      – ilkkachu
                      Dec 7 at 18:50










                    • Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                      – AFH
                      Dec 7 at 18:55
















                    0














                    var='Current date is $thedate'


                    Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.



                    echo $var


                    Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.






                    share|improve this answer

















                    • 1




                      If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                      – AFH
                      Dec 7 at 18:45












                    • @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                      – ilkkachu
                      Dec 7 at 18:50










                    • Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                      – AFH
                      Dec 7 at 18:55














                    0












                    0








                    0






                    var='Current date is $thedate'


                    Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.



                    echo $var


                    Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.






                    share|improve this answer












                    var='Current date is $thedate'


                    Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.



                    echo $var


                    Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 7 at 18:42









                    ilkkachu

                    590212




                    590212








                    • 1




                      If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                      – AFH
                      Dec 7 at 18:45












                    • @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                      – ilkkachu
                      Dec 7 at 18:50










                    • Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                      – AFH
                      Dec 7 at 18:55














                    • 1




                      If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                      – AFH
                      Dec 7 at 18:45












                    • @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                      – ilkkachu
                      Dec 7 at 18:50










                    • Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                      – AFH
                      Dec 7 at 18:55








                    1




                    1




                    If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                    – AFH
                    Dec 7 at 18:45






                    If you use var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.
                    – AFH
                    Dec 7 at 18:45














                    @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                    – ilkkachu
                    Dec 7 at 18:50




                    @AFH, at the time thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...
                    – ilkkachu
                    Dec 7 at 18:50












                    Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                    – AFH
                    Dec 7 at 18:55




                    Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
                    – AFH
                    Dec 7 at 18:55


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f1381718%2fbash-command-substitution-doesnt-work%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

                    Plaza Victoria

                    Brian Clough

                    Cáceres