Variable doesn't parse as stringCan you help me to understand this explanation of shell quoting?Alternatives...

What printing process is this?

Is it uncompelling to continue the story with lower stakes?

If someone else uploads my GPL'd code to Github without my permission, is that a copyright violation?

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

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

GFCI tripping on overload?

Is there a way to improve my grade after graduation?

foot-pounds of energy?

what can you do with Format View

Why should I "believe in" weak solutions to PDEs?

I was contacted by a private bank overseas to get my inheritance

In MTG, was there ever a five-color deck that worked well?

Getting Lost in the Caves of Chaos

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

What does "autolyco-sentimental" mean?

How to design an effective polearm-bow hybrid?

How can I perform a deterministic physics simulation?

Is an "are" omitted in this sentence

Why are there yellow dot stickers on the front doors of businesses in Russia?

How do the surviving Asgardians get to Earth?

Are the related objects in an SOQL query shared?

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

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

Formal mathematical definition of renormalization group flow



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







9















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.




























    9















    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.
























      9












      9








      9








      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 11 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 12 hours ago









      Ion SmeIon Sme

      484 bronze badges




      484 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


















          9














          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


























          • Tl;dr always quote your variables

            – gronostaj
            55 mins ago



















          3














          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









            9














            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


























            • Tl;dr always quote your variables

              – gronostaj
              55 mins ago
















            9














            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


























            • Tl;dr always quote your variables

              – gronostaj
              55 mins ago














            9












            9








            9







            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 11 hours ago









            steeldriversteeldriver

            77.2k12 gold badges128 silver badges206 bronze badges




            77.2k12 gold badges128 silver badges206 bronze badges
















            • Tl;dr always quote your variables

              – gronostaj
              55 mins ago



















            • Tl;dr always quote your variables

              – gronostaj
              55 mins ago

















            Tl;dr always quote your variables

            – gronostaj
            55 mins ago





            Tl;dr always quote your variables

            – gronostaj
            55 mins ago













            3














            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
































              3














              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






























                3












                3








                3







                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 10 hours ago

























                answered 11 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

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...