Trying to understand how to work with IFS

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











up vote
1
down vote

favorite












I'm trying to understand how to work with the shell variable IFS. I have the following code:



#!/bin/bash

ourpath=$PATH
oldIFS=$IFS
IFS=":"
echo "ourpath = $ourpath"
for directory in "$ourpath"; do
echo "directory = $directory"
done

IFS=$oldIFS


Running this using bash test.sh yields:



ourpath = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts
directory = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts


But I would expect directory to loop over the directories in $PATH because it should be split by :. What is going wrong here?










share|improve this question







New contributor




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























    up vote
    1
    down vote

    favorite












    I'm trying to understand how to work with the shell variable IFS. I have the following code:



    #!/bin/bash

    ourpath=$PATH
    oldIFS=$IFS
    IFS=":"
    echo "ourpath = $ourpath"
    for directory in "$ourpath"; do
    echo "directory = $directory"
    done

    IFS=$oldIFS


    Running this using bash test.sh yields:



    ourpath = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts
    directory = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts


    But I would expect directory to loop over the directories in $PATH because it should be split by :. What is going wrong here?










    share|improve this question







    New contributor




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





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to understand how to work with the shell variable IFS. I have the following code:



      #!/bin/bash

      ourpath=$PATH
      oldIFS=$IFS
      IFS=":"
      echo "ourpath = $ourpath"
      for directory in "$ourpath"; do
      echo "directory = $directory"
      done

      IFS=$oldIFS


      Running this using bash test.sh yields:



      ourpath = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts
      directory = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts


      But I would expect directory to loop over the directories in $PATH because it should be split by :. What is going wrong here?










      share|improve this question







      New contributor




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











      I'm trying to understand how to work with the shell variable IFS. I have the following code:



      #!/bin/bash

      ourpath=$PATH
      oldIFS=$IFS
      IFS=":"
      echo "ourpath = $ourpath"
      for directory in "$ourpath"; do
      echo "directory = $directory"
      done

      IFS=$oldIFS


      Running this using bash test.sh yields:



      ourpath = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts
      directory = ~/bin:/home/<user>/anaconda3/bin:/home/<user>/.local/bin:/home/<user>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:~/scripts


      But I would expect directory to loop over the directories in $PATH because it should be split by :. What is going wrong here?







      bash shell-script scripting






      share|improve this question







      New contributor




      Hunter 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




      Hunter 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






      New contributor




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









      asked 11 mins ago









      Hunter

      1062




      1062




      New contributor




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





      New contributor





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






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




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          It's the exact same as with space: You have quoted $directory and it doesn't get split. If you remove the quotes then it will give you the result you're looking for.



          I.e:



          for directory in $ourpath ; do
          echo "directory = $directory"
          done


          Bonus points: It won't matter if $ourpath contains spaces because IFS is :, so it will only be split over :





          share




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Hunter 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%2funix.stackexchange.com%2fquestions%2f475341%2ftrying-to-understand-how-to-work-with-ifs%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            It's the exact same as with space: You have quoted $directory and it doesn't get split. If you remove the quotes then it will give you the result you're looking for.



            I.e:



            for directory in $ourpath ; do
            echo "directory = $directory"
            done


            Bonus points: It won't matter if $ourpath contains spaces because IFS is :, so it will only be split over :





            share
























              up vote
              1
              down vote













              It's the exact same as with space: You have quoted $directory and it doesn't get split. If you remove the quotes then it will give you the result you're looking for.



              I.e:



              for directory in $ourpath ; do
              echo "directory = $directory"
              done


              Bonus points: It won't matter if $ourpath contains spaces because IFS is :, so it will only be split over :





              share






















                up vote
                1
                down vote










                up vote
                1
                down vote









                It's the exact same as with space: You have quoted $directory and it doesn't get split. If you remove the quotes then it will give you the result you're looking for.



                I.e:



                for directory in $ourpath ; do
                echo "directory = $directory"
                done


                Bonus points: It won't matter if $ourpath contains spaces because IFS is :, so it will only be split over :





                share












                It's the exact same as with space: You have quoted $directory and it doesn't get split. If you remove the quotes then it will give you the result you're looking for.



                I.e:



                for directory in $ourpath ; do
                echo "directory = $directory"
                done


                Bonus points: It won't matter if $ourpath contains spaces because IFS is :, so it will only be split over :






                share











                share


                share










                answered 9 mins ago









                V13

                2,240612




                2,240612




















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









                     

                    draft saved


                    draft discarded


















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












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











                    Hunter 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%2funix.stackexchange.com%2fquestions%2f475341%2ftrying-to-understand-how-to-work-with-ifs%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay