Trying to understand how to work with IFS

Hunter

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?

V13

It's the exact same as with space: You have quoted $ourpath 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 :

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to understand how does the AWS scaling work

Trying to understand how describe_instances() work

I'm trying to understand How a sequence of fork() function work?

Spring Kafka- Trying to understand how things work behind the scenes

Trying to understand how series work when scoped to a function in Pine Script

Trying to understand how range-based for loops work in C++

Trying to understand how SyncLock Works

Trying to understand how to make a UpdateOrCreate

Trying to understand how partitions function…

Trying to understand this function from Go, why make a function that always run in constant time and how does this work?

Having issues trying to understand how callback queries work in python-telegram-bot

How does the 'not' keyword work in this code? I'm trying to understand if 'not' reverses the value of 'out_of_guesses'

How I make my count ifs function for certain worksheets work?

Trying to understand how to fit Backbone into this menu

Trying to understand how an overloaded function is chosen

Trying to understand how some function works

Trying to understand how AngularJS $parse service works

Trying to understand how to use Angular-mock

VBA - Trying to understand how to call Class Modules

Trying to understand how to rename a byte array in Go

trying to understand how the Go gob encoder works

Trying to understand how promisification works with BlueBird

trying to understand how a cname affect the url

Trying to understand how Samba handles UNIX permissions

Trying to understand how not to add elements to a list

Trying to understand how *ramVectorTable gets to ramVectorTable[ ]

Trying to understand how these if else statements close correctly

Trying to understand how "module" & "moduleResolution" differ,

Trying to understand how does regularizer works