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?

What are the limitations of the Hendersson-Hasselbalch equation?

Piece de Resistance - Introduction & Ace and A's

Is unspent vacation time a good argument in pay negotiations?

Awk to get all my regular users in shadow

Make lens aperture in Tikz

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

What is the reason behind water not falling from a bucket at the top of loop?

Is space radiation a risk for space film photography, and how is this prevented?

Broken bottom bracket?

Properties: Left of the colon

Is a switch from R to Python worth it?

Why do proponents of guns oppose gun competency tests?

What license to choose for my PhD thesis?

What could prevent players from leaving an island?

How does Geralt transport his swords?

Is there a command-line tool for converting html files to pdf?

Vectorised way to calculate mean of left and right neighbours in a vector

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

Why do my fried eggs start browning very fast?

Can the Cauchy product of divergent series with itself be convergent?

Why do rocket engines use nitrogen actuators to operate the fuel/oxidiser valves instead of electric servos?

What does C++ language definition say about the extent of the static keyword?

On the consistency of different well-polished astronomy software

Did Logical Positivism fail because it simply denied human emotion?



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 6 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

                              ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                              Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                              19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу