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
2
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
    2
    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
      2
      down vote

      favorite









      up vote
      2
      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 27 mins ago









      Hunter

      1134




      1134




      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
          3
          down vote



          accepted










          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|improve this answer




















          • Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
            – Hunter
            11 mins ago











          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
          3
          down vote



          accepted










          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|improve this answer




















          • Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
            – Hunter
            11 mins ago















          up vote
          3
          down vote



          accepted










          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|improve this answer




















          • Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
            – Hunter
            11 mins ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          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|improve this answer












          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|improve this answer












          share|improve this answer



          share|improve this answer










          answered 25 mins ago









          V13

          2,275612




          2,275612











          • Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
            – Hunter
            11 mins ago

















          • Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
            – Hunter
            11 mins ago
















          Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
          – Hunter
          11 mins ago





          Thank you! It works now. I got this (simplified) example from "Wicked Cool Shell Scripts: 101 Scripts for Linux, OS X, and UNIX Systems" by Dave Taylor and Brandon Perry (page 11), where they also quote ourpath. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
          – Hunter
          11 mins ago











          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













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery