Variable doesn't parse as stringCan you help me to understand this explanation of shell quoting?Alternatives for [variable = string substitution] in bashScript to extract text using grepBash script — store `curl` output in variable, then format against string in variablebash script to run a second command with select output from first command as variableHow to get BASH to use * wildcard in command?Extract specific field in matrixgnome-terminal hides ending characters of the current path's first rowParse json object to bashVariable for absolute path and Alias for a command in one string or scrip?

Variable doesn't parse as string

Write The Shortest Program to Calculate Height of a Binary Tree

split inside flalign

How do people drown while wearing a life jacket?

The Game of the Century - why didn't Byrne take the rook after he forked Fischer?

Is unspent vacation time a good argument in pay negotiations?

Why do proponents of guns oppose gun competency tests?

Formal mathematical definition of renormalization group flow

Upper Bound for a Sum

Why does capacitance not depend on the material of the plates?

How to increase Solr JVM memory

Did Logical Positivism fail because it simply denied human emotion?

Need reasons why a satellite network would not work

Broken bottom bracket?

Is it okay to use different fingers every time while playing a song on keyboard? Is it considered a bad practice?

Would the shaking of an earthquake be visible to somebody in a low-flying aircraft?

Conditional probability of dependent random variables

ZFS on Linux: Which mountpoint option when mounting manually per script?

Is there a general term for the items in a directory?

What's "halachic" about "Esav hates Ya'akov"?

Is there a booking app or site that lets you specify your gender for shared dormitories?

How to call made-up data?

Why did the US Airways Flight 1549 passengers stay on the wings?

On the consistency of different well-polished astronomy software



Variable doesn't parse as string


Can you help me to understand this explanation of shell quoting?Alternatives for [variable = string substitution] in bashScript to extract text using grepBash script — store `curl` output in variable, then format against string in variablebash script to run a second command with select output from first command as variableHow to get BASH to use * wildcard in command?Extract specific field in matrixgnome-terminal hides ending characters of the current path's first rowParse json object to bashVariable for absolute path and Alias for a command in one string or scrip?






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








6















Problem



I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):



string="$(iwconfig wlan0 | grep -I Signal)"


If I echo $string, I get what I expect:



Link Quality=63/70 Signal level=-47dBm


But if I try to get a substring, echo $string:5, it returns the same thing.




Debuging:



If I paste the output of wconfig wlan0 | grep -I Signal directly into the variable: string="Link Quality=63/70 Signal level=-47dBm" then everything works as expected.



echo $string 
# Link Quality=63/70 Signal level=-47dBm
echo $string:5
# Quality=63/70 Signal level=-47dBm



Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?










share|improve this question









New contributor



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



























    6















    Problem



    I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):



    string="$(iwconfig wlan0 | grep -I Signal)"


    If I echo $string, I get what I expect:



    Link Quality=63/70 Signal level=-47dBm


    But if I try to get a substring, echo $string:5, it returns the same thing.




    Debuging:



    If I paste the output of wconfig wlan0 | grep -I Signal directly into the variable: string="Link Quality=63/70 Signal level=-47dBm" then everything works as expected.



    echo $string 
    # Link Quality=63/70 Signal level=-47dBm
    echo $string:5
    # Quality=63/70 Signal level=-47dBm



    Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?










    share|improve this question









    New contributor



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























      6












      6








      6








      Problem



      I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):



      string="$(iwconfig wlan0 | grep -I Signal)"


      If I echo $string, I get what I expect:



      Link Quality=63/70 Signal level=-47dBm


      But if I try to get a substring, echo $string:5, it returns the same thing.




      Debuging:



      If I paste the output of wconfig wlan0 | grep -I Signal directly into the variable: string="Link Quality=63/70 Signal level=-47dBm" then everything works as expected.



      echo $string 
      # Link Quality=63/70 Signal level=-47dBm
      echo $string:5
      # Quality=63/70 Signal level=-47dBm



      Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?










      share|improve this question









      New contributor



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











      Problem



      I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):



      string="$(iwconfig wlan0 | grep -I Signal)"


      If I echo $string, I get what I expect:



      Link Quality=63/70 Signal level=-47dBm


      But if I try to get a substring, echo $string:5, it returns the same thing.




      Debuging:



      If I paste the output of wconfig wlan0 | grep -I Signal directly into the variable: string="Link Quality=63/70 Signal level=-47dBm" then everything works as expected.



      echo $string 
      # Link Quality=63/70 Signal level=-47dBm
      echo $string:5
      # Quality=63/70 Signal level=-47dBm



      Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?







      command-line bash scripts text






      share|improve this question









      New contributor



      Ion Sme 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



      Ion Sme 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 8 hours ago









      user3140225

      6633 silver badges17 bronze badges




      6633 silver badges17 bronze badges






      New contributor



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








      asked 8 hours ago









      Ion SmeIon Sme

      334 bronze badges




      334 bronze badges




      New contributor



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




      New contributor




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

























          2 Answers
          2






          active

          oldest

          votes


















          5














          This is because echo $string doesn't simply print the value of string; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:



          $ string=$(iwconfig wls1 | grep Signal)
          $ echo $string
          Link Quality=38/70 Signal level=-72 dBm


          whereas



          $ echo "$string"
          Link Quality=38/70 Signal level=-72 dBm


          We can see that there is a sequence of whitespace characters at the start of $string - in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo also elides:



          $ echo "$string:5"
          Link Quality=38/70 Signal level=-72 dBm
          $ echo $string:5
          Link Quality=38/70 Signal level=-72 dBm


          For additional discussion see:



          • When is double-quoting necessary?


          • Why does my shell script choke on whitespace or other special characters?






          share|improve this answer
































            2














            This works on my machine:



            $ string="$(iwconfig wlp60s0 | grep -I Signal)"
            $ echo $string
            Link Quality=68/70 Signal level=-42 dBm
            $ echo $string | cut -d' ' -f4,5
            level=-42 dBm


            • For your machine replace wlp60s0 with wlan0.


            • Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between 42 and dBm. As such the cut command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.

            You could also use:



            $ echo $string | cut -d'=' -f3
            -42 dBm


            • In this case cut is told to use = as field delimiter.

            If you want to use $string... though the correct syntax is:



            $ echo $string##*=
            -38 dBm

            $ echo "$string##*="
            -38 dBm


            Either method will work to take the substring after the last =. The original method of 5 in your question I don't understand how it can work.






            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
              );



              );






              Ion Sme 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%2f1163602%2fvariable-doesnt-parse-as-string%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









              5














              This is because echo $string doesn't simply print the value of string; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:



              $ string=$(iwconfig wls1 | grep Signal)
              $ echo $string
              Link Quality=38/70 Signal level=-72 dBm


              whereas



              $ echo "$string"
              Link Quality=38/70 Signal level=-72 dBm


              We can see that there is a sequence of whitespace characters at the start of $string - in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo also elides:



              $ echo "$string:5"
              Link Quality=38/70 Signal level=-72 dBm
              $ echo $string:5
              Link Quality=38/70 Signal level=-72 dBm


              For additional discussion see:



              • When is double-quoting necessary?


              • Why does my shell script choke on whitespace or other special characters?






              share|improve this answer





























                5














                This is because echo $string doesn't simply print the value of string; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:



                $ string=$(iwconfig wls1 | grep Signal)
                $ echo $string
                Link Quality=38/70 Signal level=-72 dBm


                whereas



                $ echo "$string"
                Link Quality=38/70 Signal level=-72 dBm


                We can see that there is a sequence of whitespace characters at the start of $string - in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo also elides:



                $ echo "$string:5"
                Link Quality=38/70 Signal level=-72 dBm
                $ echo $string:5
                Link Quality=38/70 Signal level=-72 dBm


                For additional discussion see:



                • When is double-quoting necessary?


                • Why does my shell script choke on whitespace or other special characters?






                share|improve this answer



























                  5












                  5








                  5







                  This is because echo $string doesn't simply print the value of string; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:



                  $ string=$(iwconfig wls1 | grep Signal)
                  $ echo $string
                  Link Quality=38/70 Signal level=-72 dBm


                  whereas



                  $ echo "$string"
                  Link Quality=38/70 Signal level=-72 dBm


                  We can see that there is a sequence of whitespace characters at the start of $string - in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo also elides:



                  $ echo "$string:5"
                  Link Quality=38/70 Signal level=-72 dBm
                  $ echo $string:5
                  Link Quality=38/70 Signal level=-72 dBm


                  For additional discussion see:



                  • When is double-quoting necessary?


                  • Why does my shell script choke on whitespace or other special characters?






                  share|improve this answer













                  This is because echo $string doesn't simply print the value of string; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:



                  $ string=$(iwconfig wls1 | grep Signal)
                  $ echo $string
                  Link Quality=38/70 Signal level=-72 dBm


                  whereas



                  $ echo "$string"
                  Link Quality=38/70 Signal level=-72 dBm


                  We can see that there is a sequence of whitespace characters at the start of $string - in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo also elides:



                  $ echo "$string:5"
                  Link Quality=38/70 Signal level=-72 dBm
                  $ echo $string:5
                  Link Quality=38/70 Signal level=-72 dBm


                  For additional discussion see:



                  • When is double-quoting necessary?


                  • Why does my shell script choke on whitespace or other special characters?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 8 hours ago









                  steeldriversteeldriver

                  77.2k12 gold badges128 silver badges206 bronze badges




                  77.2k12 gold badges128 silver badges206 bronze badges


























                      2














                      This works on my machine:



                      $ string="$(iwconfig wlp60s0 | grep -I Signal)"
                      $ echo $string
                      Link Quality=68/70 Signal level=-42 dBm
                      $ echo $string | cut -d' ' -f4,5
                      level=-42 dBm


                      • For your machine replace wlp60s0 with wlan0.


                      • Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between 42 and dBm. As such the cut command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.

                      You could also use:



                      $ echo $string | cut -d'=' -f3
                      -42 dBm


                      • In this case cut is told to use = as field delimiter.

                      If you want to use $string... though the correct syntax is:



                      $ echo $string##*=
                      -38 dBm

                      $ echo "$string##*="
                      -38 dBm


                      Either method will work to take the substring after the last =. The original method of 5 in your question I don't understand how it can work.






                      share|improve this answer































                        2














                        This works on my machine:



                        $ string="$(iwconfig wlp60s0 | grep -I Signal)"
                        $ echo $string
                        Link Quality=68/70 Signal level=-42 dBm
                        $ echo $string | cut -d' ' -f4,5
                        level=-42 dBm


                        • For your machine replace wlp60s0 with wlan0.


                        • Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between 42 and dBm. As such the cut command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.

                        You could also use:



                        $ echo $string | cut -d'=' -f3
                        -42 dBm


                        • In this case cut is told to use = as field delimiter.

                        If you want to use $string... though the correct syntax is:



                        $ echo $string##*=
                        -38 dBm

                        $ echo "$string##*="
                        -38 dBm


                        Either method will work to take the substring after the last =. The original method of 5 in your question I don't understand how it can work.






                        share|improve this answer





























                          2












                          2








                          2







                          This works on my machine:



                          $ string="$(iwconfig wlp60s0 | grep -I Signal)"
                          $ echo $string
                          Link Quality=68/70 Signal level=-42 dBm
                          $ echo $string | cut -d' ' -f4,5
                          level=-42 dBm


                          • For your machine replace wlp60s0 with wlan0.


                          • Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between 42 and dBm. As such the cut command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.

                          You could also use:



                          $ echo $string | cut -d'=' -f3
                          -42 dBm


                          • In this case cut is told to use = as field delimiter.

                          If you want to use $string... though the correct syntax is:



                          $ echo $string##*=
                          -38 dBm

                          $ echo "$string##*="
                          -38 dBm


                          Either method will work to take the substring after the last =. The original method of 5 in your question I don't understand how it can work.






                          share|improve this answer















                          This works on my machine:



                          $ string="$(iwconfig wlp60s0 | grep -I Signal)"
                          $ echo $string
                          Link Quality=68/70 Signal level=-42 dBm
                          $ echo $string | cut -d' ' -f4,5
                          level=-42 dBm


                          • For your machine replace wlp60s0 with wlan0.


                          • Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between 42 and dBm. As such the cut command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.

                          You could also use:



                          $ echo $string | cut -d'=' -f3
                          -42 dBm


                          • In this case cut is told to use = as field delimiter.

                          If you want to use $string... though the correct syntax is:



                          $ echo $string##*=
                          -38 dBm

                          $ echo "$string##*="
                          -38 dBm


                          Either method will work to take the substring after the last =. The original method of 5 in your question I don't understand how it can work.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 7 hours ago

























                          answered 8 hours ago









                          WinEunuuchs2UnixWinEunuuchs2Unix

                          56.1k16 gold badges109 silver badges217 bronze badges




                          56.1k16 gold badges109 silver badges217 bronze badges























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









                              draft saved

                              draft discarded


















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












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











                              Ion Sme 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%2f1163602%2fvariable-doesnt-parse-as-string%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

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單