Variable with quotation marks “$()” The 2019 Stack Overflow Developer Survey Results Are InWhat is the point of the bash Null-operator “:”, colon?Why does bash remove n in $(cat file)?gdialog cannot output variablefind does not work with my variablebash variable array names and getting valuesDefining and incrementing a variable in bashRename directory using a variableFor loop with variable incrementusing variable within quotation marksQuotation Mark-InceptionUsing bash in combination with psql, passing variable between themAssigned variable for pipeline is not working

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

The phrase "to the numbers born"?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Is bread bad for ducks?

What is this business jet?

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?

If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?

Can you cast a spell on someone in the Ethereal Plane, if you are on the Material Plane and have the True Seeing spell active?

What is the meaning of Triage in Cybersec world?

How to charge AirPods to keep battery healthy?

Why is this code so slow?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Is it okay to consider publishing in my first year of PhD?

Output the Arecibo Message

Dropping list elements from nested list after evaluation

Why isn't the circumferential light around the M87 black hole's event horizon symmetric?

What must someone know in statistics and machine learning?

Why does the nucleus not repel itself?

If climate change impact can be observed in nature, has that had any effect on rural, i.e. farming community, perception of the scientific consensus?

Loose spokes after only a few rides

How to support a colleague who finds meetings extremely tiring?

The difference between dialogue marks

"as much details as you can remember"

Match Roman Numerals



Variable with quotation marks “$()”



The 2019 Stack Overflow Developer Survey Results Are InWhat is the point of the bash Null-operator “:”, colon?Why does bash remove n in $(cat file)?gdialog cannot output variablefind does not work with my variablebash variable array names and getting valuesDefining and incrementing a variable in bashRename directory using a variableFor loop with variable incrementusing variable within quotation marksQuotation Mark-InceptionUsing bash in combination with psql, passing variable between themAssigned variable for pipeline is not working



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Previously a member @Evan Chen, wrote this script :



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done


I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.



Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?



To make a nested command ??



Thanks










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    1















    Previously a member @Evan Chen, wrote this script :



    #!/bin/bash
    while [ true ]
    do
    currentoutput="$(lsusb)"
    if [ "$currentoutput" != "$lastoutput" ]
    then
    echo "" date and Time >> test.log
    date +%x_r >> test.log
    lastoutput="$(lsusb)"
    lsusb >> test.log
    fi
    sleep 5
    done


    I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.



    Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?



    To make a nested command ??



    Thanks










    share|improve this question









    New contributor




    Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      Previously a member @Evan Chen, wrote this script :



      #!/bin/bash
      while [ true ]
      do
      currentoutput="$(lsusb)"
      if [ "$currentoutput" != "$lastoutput" ]
      then
      echo "" date and Time >> test.log
      date +%x_r >> test.log
      lastoutput="$(lsusb)"
      lsusb >> test.log
      fi
      sleep 5
      done


      I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.



      Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?



      To make a nested command ??



      Thanks










      share|improve this question









      New contributor




      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      Previously a member @Evan Chen, wrote this script :



      #!/bin/bash
      while [ true ]
      do
      currentoutput="$(lsusb)"
      if [ "$currentoutput" != "$lastoutput" ]
      then
      echo "" date and Time >> test.log
      date +%x_r >> test.log
      lastoutput="$(lsusb)"
      lsusb >> test.log
      fi
      sleep 5
      done


      I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.



      Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?



      To make a nested command ??



      Thanks







      bash scripts






      share|improve this question









      New contributor




      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 57 mins ago







      Shankhara













      New contributor




      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      ShankharaShankhara

      62




      62




      New contributor




      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes


















          2














          In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



          Older syntax for this was



          currentoutput=`lsusb`


          you can find it in many examples and scripts



          To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






          share|improve this answer






























            2














            The following runs the external command command and returns its output.



            "$(command)"


            Without the brackets/parentheses, this would look for a variable instead of running a command:



            "$variable"


            As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






            share|improve this answer

























            • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

              – Shankhara
              58 mins ago


















            0














            Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS shell variable).



            For example,



            $ var="one two"
            $ howmany() echo $#;
            $ howmany $var
            2
            $ howmany "$var"
            1


            Here we define howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



            This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var variable, touch $var would create two files, but touch "$var" just one.



            Same goes for [ "$currentoutput" != "$lastoutput" ] part. This particular test performs comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ commands sees exactly those 3 arguments. Now what happens if variables are unquoted ?



            $ var="hello world"
            $ foo="hi world"
            $ [ $var != $foo ]
            bash: [: too many arguments
            $


            Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, contents of variables are understood as separate units rather than one whole item.



            Assigning command substitution doesn't require double quotes as in



            var=$( df )


            where you have df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




            On side note, the



            while [ true ]


            part can be



            while true


            [ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



            The double quotes echo "" date and Time part could probably be removed. They merely insert empty string and add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



            lsusb >> test.log


            This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been ran already in currentoutput=$(lsusb). In cases where trailing newlines
            have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




            See also:



            • When is double-quoting necessary?

            • What is the point of the bash null operator : (colon)?





            share|improve this answer

























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "89"
              ;
              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
              );



              );






              Shankhara is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



              Older syntax for this was



              currentoutput=`lsusb`


              you can find it in many examples and scripts



              To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






              share|improve this answer



























                2














                In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



                Older syntax for this was



                currentoutput=`lsusb`


                you can find it in many examples and scripts



                To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






                share|improve this answer

























                  2












                  2








                  2







                  In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



                  Older syntax for this was



                  currentoutput=`lsusb`


                  you can find it in many examples and scripts



                  To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






                  share|improve this answer













                  In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



                  Older syntax for this was



                  currentoutput=`lsusb`


                  you can find it in many examples and scripts



                  To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 58 mins ago









                  marosgmarosg

                  46437




                  46437























                      2














                      The following runs the external command command and returns its output.



                      "$(command)"


                      Without the brackets/parentheses, this would look for a variable instead of running a command:



                      "$variable"


                      As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






                      share|improve this answer

























                      • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                        – Shankhara
                        58 mins ago















                      2














                      The following runs the external command command and returns its output.



                      "$(command)"


                      Without the brackets/parentheses, this would look for a variable instead of running a command:



                      "$variable"


                      As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






                      share|improve this answer

























                      • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                        – Shankhara
                        58 mins ago













                      2












                      2








                      2







                      The following runs the external command command and returns its output.



                      "$(command)"


                      Without the brackets/parentheses, this would look for a variable instead of running a command:



                      "$variable"


                      As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






                      share|improve this answer















                      The following runs the external command command and returns its output.



                      "$(command)"


                      Without the brackets/parentheses, this would look for a variable instead of running a command:



                      "$variable"


                      As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 57 mins ago

























                      answered 1 hour ago









                      thomasrutterthomasrutter

                      27.3k47089




                      27.3k47089












                      • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                        – Shankhara
                        58 mins ago

















                      • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                        – Shankhara
                        58 mins ago
















                      HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                      – Shankhara
                      58 mins ago





                      HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

                      – Shankhara
                      58 mins ago











                      0














                      Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS shell variable).



                      For example,



                      $ var="one two"
                      $ howmany() echo $#;
                      $ howmany $var
                      2
                      $ howmany "$var"
                      1


                      Here we define howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



                      This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var variable, touch $var would create two files, but touch "$var" just one.



                      Same goes for [ "$currentoutput" != "$lastoutput" ] part. This particular test performs comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ commands sees exactly those 3 arguments. Now what happens if variables are unquoted ?



                      $ var="hello world"
                      $ foo="hi world"
                      $ [ $var != $foo ]
                      bash: [: too many arguments
                      $


                      Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, contents of variables are understood as separate units rather than one whole item.



                      Assigning command substitution doesn't require double quotes as in



                      var=$( df )


                      where you have df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




                      On side note, the



                      while [ true ]


                      part can be



                      while true


                      [ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



                      The double quotes echo "" date and Time part could probably be removed. They merely insert empty string and add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



                      lsusb >> test.log


                      This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been ran already in currentoutput=$(lsusb). In cases where trailing newlines
                      have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




                      See also:



                      • When is double-quoting necessary?

                      • What is the point of the bash null operator : (colon)?





                      share|improve this answer





























                        0














                        Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS shell variable).



                        For example,



                        $ var="one two"
                        $ howmany() echo $#;
                        $ howmany $var
                        2
                        $ howmany "$var"
                        1


                        Here we define howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



                        This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var variable, touch $var would create two files, but touch "$var" just one.



                        Same goes for [ "$currentoutput" != "$lastoutput" ] part. This particular test performs comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ commands sees exactly those 3 arguments. Now what happens if variables are unquoted ?



                        $ var="hello world"
                        $ foo="hi world"
                        $ [ $var != $foo ]
                        bash: [: too many arguments
                        $


                        Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, contents of variables are understood as separate units rather than one whole item.



                        Assigning command substitution doesn't require double quotes as in



                        var=$( df )


                        where you have df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




                        On side note, the



                        while [ true ]


                        part can be



                        while true


                        [ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



                        The double quotes echo "" date and Time part could probably be removed. They merely insert empty string and add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



                        lsusb >> test.log


                        This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been ran already in currentoutput=$(lsusb). In cases where trailing newlines
                        have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




                        See also:



                        • When is double-quoting necessary?

                        • What is the point of the bash null operator : (colon)?





                        share|improve this answer



























                          0












                          0








                          0







                          Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS shell variable).



                          For example,



                          $ var="one two"
                          $ howmany() echo $#;
                          $ howmany $var
                          2
                          $ howmany "$var"
                          1


                          Here we define howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



                          This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var variable, touch $var would create two files, but touch "$var" just one.



                          Same goes for [ "$currentoutput" != "$lastoutput" ] part. This particular test performs comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ commands sees exactly those 3 arguments. Now what happens if variables are unquoted ?



                          $ var="hello world"
                          $ foo="hi world"
                          $ [ $var != $foo ]
                          bash: [: too many arguments
                          $


                          Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, contents of variables are understood as separate units rather than one whole item.



                          Assigning command substitution doesn't require double quotes as in



                          var=$( df )


                          where you have df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




                          On side note, the



                          while [ true ]


                          part can be



                          while true


                          [ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



                          The double quotes echo "" date and Time part could probably be removed. They merely insert empty string and add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



                          lsusb >> test.log


                          This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been ran already in currentoutput=$(lsusb). In cases where trailing newlines
                          have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




                          See also:



                          • When is double-quoting necessary?

                          • What is the point of the bash null operator : (colon)?





                          share|improve this answer















                          Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS shell variable).



                          For example,



                          $ var="one two"
                          $ howmany() echo $#;
                          $ howmany $var
                          2
                          $ howmany "$var"
                          1


                          Here we define howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



                          This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var variable, touch $var would create two files, but touch "$var" just one.



                          Same goes for [ "$currentoutput" != "$lastoutput" ] part. This particular test performs comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ commands sees exactly those 3 arguments. Now what happens if variables are unquoted ?



                          $ var="hello world"
                          $ foo="hi world"
                          $ [ $var != $foo ]
                          bash: [: too many arguments
                          $


                          Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, contents of variables are understood as separate units rather than one whole item.



                          Assigning command substitution doesn't require double quotes as in



                          var=$( df )


                          where you have df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




                          On side note, the



                          while [ true ]


                          part can be



                          while true


                          [ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



                          The double quotes echo "" date and Time part could probably be removed. They merely insert empty string and add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



                          lsusb >> test.log


                          This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been ran already in currentoutput=$(lsusb). In cases where trailing newlines
                          have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




                          See also:



                          • When is double-quoting necessary?

                          • What is the point of the bash null operator : (colon)?






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 1 min ago

























                          answered 25 mins ago









                          Sergiy KolodyazhnyySergiy Kolodyazhnyy

                          75.1k9155327




                          75.1k9155327




















                              Shankhara is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              Shankhara is a new contributor. Be nice, and check out our Code of Conduct.












                              Shankhara is a new contributor. Be nice, and check out our Code of Conduct.











                              Shankhara is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Ask Ubuntu!


                              • 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%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%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

                              名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                              Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                              Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar