Trying to understand how to work with IFS
Clash 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?
bash shell-script scripting
New contributor
add a comment |Â
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?
bash shell-script scripting
New contributor
add a comment |Â
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?
bash shell-script scripting
New contributor
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
bash shell-script scripting
New contributor
New contributor
New contributor
asked 11 mins ago
Hunter
1062
1062
New contributor
New contributor
add a comment |Â
add a comment |Â
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 :
add a comment |Â
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 :
add a comment |Â
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 :
add a comment |Â
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 :
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 :
answered 9 mins ago
V13
2,240612
2,240612
add a comment |Â
add a comment |Â
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.
Hunter is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password