back ticks vs double quotes

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











up vote
6
down vote

favorite












I've been wondering this for a long time but haven't figured out how to look it up -



is this:



x=`command -v r2g`


the same as this:



x="$(command -v r2g)"


or is it the same as this:



x=$(command -v r2g)


...if it's the latter, should I do this to fix it?



x="`command -v r2g`"






share|improve this question

























    up vote
    6
    down vote

    favorite












    I've been wondering this for a long time but haven't figured out how to look it up -



    is this:



    x=`command -v r2g`


    the same as this:



    x="$(command -v r2g)"


    or is it the same as this:



    x=$(command -v r2g)


    ...if it's the latter, should I do this to fix it?



    x="`command -v r2g`"






    share|improve this question























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I've been wondering this for a long time but haven't figured out how to look it up -



      is this:



      x=`command -v r2g`


      the same as this:



      x="$(command -v r2g)"


      or is it the same as this:



      x=$(command -v r2g)


      ...if it's the latter, should I do this to fix it?



      x="`command -v r2g`"






      share|improve this question













      I've been wondering this for a long time but haven't figured out how to look it up -



      is this:



      x=`command -v r2g`


      the same as this:



      x="$(command -v r2g)"


      or is it the same as this:



      x=$(command -v r2g)


      ...if it's the latter, should I do this to fix it?



      x="`command -v r2g`"








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 28 at 7:17
























      asked May 28 at 3:54









      Alexander Mills

      1,878929




      1,878929




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          7
          down vote













          All examples are of variable assignment from command substitution, so they are equivalent. As per Gilles's answer, quoting isn't necessary on right hand of assignment to variable, since word splitting doesn't occur there. So all four are OK.



          If they were standalone, i.e. not in assignment, then you'd need to quote. The $(...) form compared to backticks has advantage that quotes can be nested and broken into multiple lines, which is why this form is generally preferred nowadays. In other words, you can do "$( echo "$var" )" with this form to protect both the inner expansion of $var and the outer expansion of $(...) from word splitting and filename globbing.



          As shown in POSIX Shell Command Language specs, embedded multiline scripts don't work with backticks (on the left), but do work with $() form (on the right).



          echo ` echo $(
          cat <<eof cat <<eof
          a here-doc with ` a here-doc with )
          eof eof
          ` )


          echo ` echo $(
          echo abc # a comment with ` echo abc # a comment with )
          ` )


          echo ` echo $(
          echo '`' echo ')'
          ` )





          share|improve this answer






























            up vote
            3
            down vote













            The four examples are functionally equivalent.



            Backticks are obsolete, and unless you are using an 1970 shell like a Bourne shell (like Heirloom) you do not need them. The main problem is that they are quite difficult to nest, try:



            $ echo $(uname | $(echo cat))
            Linux

            $ echo `uname | `echo cat``
            bash: command substitution: line 2: syntax error: unexpected end of file
            echo cat


            On the rigth side of a command line with only an assignement it is not necesary (but harmless) to quote the expansion, as the expansion is considered quoted anyway:



            $ var=$(uname)


            But that is not always true, an assignment on the command export is regarded as an argument and will be split and glob in some shells (not in bash):



            $ dash -c 'export MYVAR=`echo a test`;echo "$MYVAR"'
            a


            The same reasoning apply to local (Are quotes needed for local variable assignment?) and declare (and some other).



            What you should do to "fix it", is:



            x=$(command -v r2g)


            And sometimes (for portable scripts):



            export x="$(command -v r2g)"





            share|improve this answer




























              up vote
              1
              down vote













              Yes, the backticks should also be quoted.



              This may be a matter of preferred bash style for cases where the command output does not contain spaces. Here's a quote from the author of the shellharden utility, from "how to do things safely in bash":



              Should I use backticks?
              Command substitutions also come in this form:

              Correct: "`cmd`"
              Bad: `cmd`
              While it is possible to use this style correctly, it looks even more awkward in quotes and is less readable when nested. The consensus around this one is pretty clear: Avoid.

              Shellharden rewrites these into the dollar-parenthesis form.


              I also believe it's good form to either quote backticks with ", or (better) rewrite it to use $(). If the command output contains spaces or special caracters when using backticks, it may be problematic if not quoting the expression.






              share|improve this answer























              • can you quote the section of the article that discusses this?
                – Alexander Mills
                May 28 at 4:25










              • You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                – Kusalananda
                May 28 at 5:25










              • All command execution where the output may contain spaces should be quoted with ", I think.
                – Alexander
                May 28 at 8:10

















              up vote
              -1
              down vote













              Yes my guess looks correct, according to this document:
              https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md



              It says:



              # Should I use backticks?
              # Command substitutions also come in this form:

              Correct: "`cmd`"
              Bad: `cmd`





              share|improve this answer





















              • Moved discussion to chat.
                – Michael Mrozek♦
                May 30 at 5:57











              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f446388%2fback-ticks-vs-double-quotes%23new-answer', 'question_page');

              );

              Post as a guest






























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              7
              down vote













              All examples are of variable assignment from command substitution, so they are equivalent. As per Gilles's answer, quoting isn't necessary on right hand of assignment to variable, since word splitting doesn't occur there. So all four are OK.



              If they were standalone, i.e. not in assignment, then you'd need to quote. The $(...) form compared to backticks has advantage that quotes can be nested and broken into multiple lines, which is why this form is generally preferred nowadays. In other words, you can do "$( echo "$var" )" with this form to protect both the inner expansion of $var and the outer expansion of $(...) from word splitting and filename globbing.



              As shown in POSIX Shell Command Language specs, embedded multiline scripts don't work with backticks (on the left), but do work with $() form (on the right).



              echo ` echo $(
              cat <<eof cat <<eof
              a here-doc with ` a here-doc with )
              eof eof
              ` )


              echo ` echo $(
              echo abc # a comment with ` echo abc # a comment with )
              ` )


              echo ` echo $(
              echo '`' echo ')'
              ` )





              share|improve this answer



























                up vote
                7
                down vote













                All examples are of variable assignment from command substitution, so they are equivalent. As per Gilles's answer, quoting isn't necessary on right hand of assignment to variable, since word splitting doesn't occur there. So all four are OK.



                If they were standalone, i.e. not in assignment, then you'd need to quote. The $(...) form compared to backticks has advantage that quotes can be nested and broken into multiple lines, which is why this form is generally preferred nowadays. In other words, you can do "$( echo "$var" )" with this form to protect both the inner expansion of $var and the outer expansion of $(...) from word splitting and filename globbing.



                As shown in POSIX Shell Command Language specs, embedded multiline scripts don't work with backticks (on the left), but do work with $() form (on the right).



                echo ` echo $(
                cat <<eof cat <<eof
                a here-doc with ` a here-doc with )
                eof eof
                ` )


                echo ` echo $(
                echo abc # a comment with ` echo abc # a comment with )
                ` )


                echo ` echo $(
                echo '`' echo ')'
                ` )





                share|improve this answer

























                  up vote
                  7
                  down vote










                  up vote
                  7
                  down vote









                  All examples are of variable assignment from command substitution, so they are equivalent. As per Gilles's answer, quoting isn't necessary on right hand of assignment to variable, since word splitting doesn't occur there. So all four are OK.



                  If they were standalone, i.e. not in assignment, then you'd need to quote. The $(...) form compared to backticks has advantage that quotes can be nested and broken into multiple lines, which is why this form is generally preferred nowadays. In other words, you can do "$( echo "$var" )" with this form to protect both the inner expansion of $var and the outer expansion of $(...) from word splitting and filename globbing.



                  As shown in POSIX Shell Command Language specs, embedded multiline scripts don't work with backticks (on the left), but do work with $() form (on the right).



                  echo ` echo $(
                  cat <<eof cat <<eof
                  a here-doc with ` a here-doc with )
                  eof eof
                  ` )


                  echo ` echo $(
                  echo abc # a comment with ` echo abc # a comment with )
                  ` )


                  echo ` echo $(
                  echo '`' echo ')'
                  ` )





                  share|improve this answer















                  All examples are of variable assignment from command substitution, so they are equivalent. As per Gilles's answer, quoting isn't necessary on right hand of assignment to variable, since word splitting doesn't occur there. So all four are OK.



                  If they were standalone, i.e. not in assignment, then you'd need to quote. The $(...) form compared to backticks has advantage that quotes can be nested and broken into multiple lines, which is why this form is generally preferred nowadays. In other words, you can do "$( echo "$var" )" with this form to protect both the inner expansion of $var and the outer expansion of $(...) from word splitting and filename globbing.



                  As shown in POSIX Shell Command Language specs, embedded multiline scripts don't work with backticks (on the left), but do work with $() form (on the right).



                  echo ` echo $(
                  cat <<eof cat <<eof
                  a here-doc with ` a here-doc with )
                  eof eof
                  ` )


                  echo ` echo $(
                  echo abc # a comment with ` echo abc # a comment with )
                  ` )


                  echo ` echo $(
                  echo '`' echo ')'
                  ` )






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited May 28 at 6:36









                  Isaac

                  6,3241633




                  6,3241633











                  answered May 28 at 5:06









                  Sergiy Kolodyazhnyy

                  7,56611545




                  7,56611545






















                      up vote
                      3
                      down vote













                      The four examples are functionally equivalent.



                      Backticks are obsolete, and unless you are using an 1970 shell like a Bourne shell (like Heirloom) you do not need them. The main problem is that they are quite difficult to nest, try:



                      $ echo $(uname | $(echo cat))
                      Linux

                      $ echo `uname | `echo cat``
                      bash: command substitution: line 2: syntax error: unexpected end of file
                      echo cat


                      On the rigth side of a command line with only an assignement it is not necesary (but harmless) to quote the expansion, as the expansion is considered quoted anyway:



                      $ var=$(uname)


                      But that is not always true, an assignment on the command export is regarded as an argument and will be split and glob in some shells (not in bash):



                      $ dash -c 'export MYVAR=`echo a test`;echo "$MYVAR"'
                      a


                      The same reasoning apply to local (Are quotes needed for local variable assignment?) and declare (and some other).



                      What you should do to "fix it", is:



                      x=$(command -v r2g)


                      And sometimes (for portable scripts):



                      export x="$(command -v r2g)"





                      share|improve this answer

























                        up vote
                        3
                        down vote













                        The four examples are functionally equivalent.



                        Backticks are obsolete, and unless you are using an 1970 shell like a Bourne shell (like Heirloom) you do not need them. The main problem is that they are quite difficult to nest, try:



                        $ echo $(uname | $(echo cat))
                        Linux

                        $ echo `uname | `echo cat``
                        bash: command substitution: line 2: syntax error: unexpected end of file
                        echo cat


                        On the rigth side of a command line with only an assignement it is not necesary (but harmless) to quote the expansion, as the expansion is considered quoted anyway:



                        $ var=$(uname)


                        But that is not always true, an assignment on the command export is regarded as an argument and will be split and glob in some shells (not in bash):



                        $ dash -c 'export MYVAR=`echo a test`;echo "$MYVAR"'
                        a


                        The same reasoning apply to local (Are quotes needed for local variable assignment?) and declare (and some other).



                        What you should do to "fix it", is:



                        x=$(command -v r2g)


                        And sometimes (for portable scripts):



                        export x="$(command -v r2g)"





                        share|improve this answer























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          The four examples are functionally equivalent.



                          Backticks are obsolete, and unless you are using an 1970 shell like a Bourne shell (like Heirloom) you do not need them. The main problem is that they are quite difficult to nest, try:



                          $ echo $(uname | $(echo cat))
                          Linux

                          $ echo `uname | `echo cat``
                          bash: command substitution: line 2: syntax error: unexpected end of file
                          echo cat


                          On the rigth side of a command line with only an assignement it is not necesary (but harmless) to quote the expansion, as the expansion is considered quoted anyway:



                          $ var=$(uname)


                          But that is not always true, an assignment on the command export is regarded as an argument and will be split and glob in some shells (not in bash):



                          $ dash -c 'export MYVAR=`echo a test`;echo "$MYVAR"'
                          a


                          The same reasoning apply to local (Are quotes needed for local variable assignment?) and declare (and some other).



                          What you should do to "fix it", is:



                          x=$(command -v r2g)


                          And sometimes (for portable scripts):



                          export x="$(command -v r2g)"





                          share|improve this answer













                          The four examples are functionally equivalent.



                          Backticks are obsolete, and unless you are using an 1970 shell like a Bourne shell (like Heirloom) you do not need them. The main problem is that they are quite difficult to nest, try:



                          $ echo $(uname | $(echo cat))
                          Linux

                          $ echo `uname | `echo cat``
                          bash: command substitution: line 2: syntax error: unexpected end of file
                          echo cat


                          On the rigth side of a command line with only an assignement it is not necesary (but harmless) to quote the expansion, as the expansion is considered quoted anyway:



                          $ var=$(uname)


                          But that is not always true, an assignment on the command export is regarded as an argument and will be split and glob in some shells (not in bash):



                          $ dash -c 'export MYVAR=`echo a test`;echo "$MYVAR"'
                          a


                          The same reasoning apply to local (Are quotes needed for local variable assignment?) and declare (and some other).



                          What you should do to "fix it", is:



                          x=$(command -v r2g)


                          And sometimes (for portable scripts):



                          export x="$(command -v r2g)"






                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered May 28 at 5:45









                          Isaac

                          6,3241633




                          6,3241633




















                              up vote
                              1
                              down vote













                              Yes, the backticks should also be quoted.



                              This may be a matter of preferred bash style for cases where the command output does not contain spaces. Here's a quote from the author of the shellharden utility, from "how to do things safely in bash":



                              Should I use backticks?
                              Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`
                              While it is possible to use this style correctly, it looks even more awkward in quotes and is less readable when nested. The consensus around this one is pretty clear: Avoid.

                              Shellharden rewrites these into the dollar-parenthesis form.


                              I also believe it's good form to either quote backticks with ", or (better) rewrite it to use $(). If the command output contains spaces or special caracters when using backticks, it may be problematic if not quoting the expression.






                              share|improve this answer























                              • can you quote the section of the article that discusses this?
                                – Alexander Mills
                                May 28 at 4:25










                              • You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                                – Kusalananda
                                May 28 at 5:25










                              • All command execution where the output may contain spaces should be quoted with ", I think.
                                – Alexander
                                May 28 at 8:10














                              up vote
                              1
                              down vote













                              Yes, the backticks should also be quoted.



                              This may be a matter of preferred bash style for cases where the command output does not contain spaces. Here's a quote from the author of the shellharden utility, from "how to do things safely in bash":



                              Should I use backticks?
                              Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`
                              While it is possible to use this style correctly, it looks even more awkward in quotes and is less readable when nested. The consensus around this one is pretty clear: Avoid.

                              Shellharden rewrites these into the dollar-parenthesis form.


                              I also believe it's good form to either quote backticks with ", or (better) rewrite it to use $(). If the command output contains spaces or special caracters when using backticks, it may be problematic if not quoting the expression.






                              share|improve this answer























                              • can you quote the section of the article that discusses this?
                                – Alexander Mills
                                May 28 at 4:25










                              • You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                                – Kusalananda
                                May 28 at 5:25










                              • All command execution where the output may contain spaces should be quoted with ", I think.
                                – Alexander
                                May 28 at 8:10












                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Yes, the backticks should also be quoted.



                              This may be a matter of preferred bash style for cases where the command output does not contain spaces. Here's a quote from the author of the shellharden utility, from "how to do things safely in bash":



                              Should I use backticks?
                              Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`
                              While it is possible to use this style correctly, it looks even more awkward in quotes and is less readable when nested. The consensus around this one is pretty clear: Avoid.

                              Shellharden rewrites these into the dollar-parenthesis form.


                              I also believe it's good form to either quote backticks with ", or (better) rewrite it to use $(). If the command output contains spaces or special caracters when using backticks, it may be problematic if not quoting the expression.






                              share|improve this answer















                              Yes, the backticks should also be quoted.



                              This may be a matter of preferred bash style for cases where the command output does not contain spaces. Here's a quote from the author of the shellharden utility, from "how to do things safely in bash":



                              Should I use backticks?
                              Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`
                              While it is possible to use this style correctly, it looks even more awkward in quotes and is less readable when nested. The consensus around this one is pretty clear: Avoid.

                              Shellharden rewrites these into the dollar-parenthesis form.


                              I also believe it's good form to either quote backticks with ", or (better) rewrite it to use $(). If the command output contains spaces or special caracters when using backticks, it may be problematic if not quoting the expression.







                              share|improve this answer















                              share|improve this answer



                              share|improve this answer








                              edited May 28 at 8:13


























                              answered May 28 at 4:08









                              Alexander

                              5,48512043




                              5,48512043











                              • can you quote the section of the article that discusses this?
                                – Alexander Mills
                                May 28 at 4:25










                              • You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                                – Kusalananda
                                May 28 at 5:25










                              • All command execution where the output may contain spaces should be quoted with ", I think.
                                – Alexander
                                May 28 at 8:10
















                              • can you quote the section of the article that discusses this?
                                – Alexander Mills
                                May 28 at 4:25










                              • You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                                – Kusalananda
                                May 28 at 5:25










                              • All command execution where the output may contain spaces should be quoted with ", I think.
                                – Alexander
                                May 28 at 8:10















                              can you quote the section of the article that discusses this?
                              – Alexander Mills
                              May 28 at 4:25




                              can you quote the section of the article that discusses this?
                              – Alexander Mills
                              May 28 at 4:25












                              You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                              – Kusalananda
                              May 28 at 5:25




                              You could argue that any expansion should always be quoted, but it's not necessary in an assignment like this.
                              – Kusalananda
                              May 28 at 5:25












                              All command execution where the output may contain spaces should be quoted with ", I think.
                              – Alexander
                              May 28 at 8:10




                              All command execution where the output may contain spaces should be quoted with ", I think.
                              – Alexander
                              May 28 at 8:10










                              up vote
                              -1
                              down vote













                              Yes my guess looks correct, according to this document:
                              https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md



                              It says:



                              # Should I use backticks?
                              # Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`





                              share|improve this answer





















                              • Moved discussion to chat.
                                – Michael Mrozek♦
                                May 30 at 5:57















                              up vote
                              -1
                              down vote













                              Yes my guess looks correct, according to this document:
                              https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md



                              It says:



                              # Should I use backticks?
                              # Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`





                              share|improve this answer





















                              • Moved discussion to chat.
                                – Michael Mrozek♦
                                May 30 at 5:57













                              up vote
                              -1
                              down vote










                              up vote
                              -1
                              down vote









                              Yes my guess looks correct, according to this document:
                              https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md



                              It says:



                              # Should I use backticks?
                              # Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`





                              share|improve this answer













                              Yes my guess looks correct, according to this document:
                              https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md



                              It says:



                              # Should I use backticks?
                              # Command substitutions also come in this form:

                              Correct: "`cmd`"
                              Bad: `cmd`






                              share|improve this answer













                              share|improve this answer



                              share|improve this answer











                              answered May 28 at 4:25









                              Alexander Mills

                              1,878929




                              1,878929











                              • Moved discussion to chat.
                                – Michael Mrozek♦
                                May 30 at 5:57

















                              • Moved discussion to chat.
                                – Michael Mrozek♦
                                May 30 at 5:57
















                              Moved discussion to chat.
                              – Michael Mrozek♦
                              May 30 at 5:57





                              Moved discussion to chat.
                              – Michael Mrozek♦
                              May 30 at 5:57













                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446388%2fback-ticks-vs-double-quotes%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              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