Trying to understand how to work with IFS
Clash 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?
bash shell-script scripting
New contributor
add a comment |Â
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?
bash shell-script scripting
New contributor
add a comment |Â
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?
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 27 mins ago
Hunter
1134
1134
New contributor
New contributor
add a comment |Â
add a comment |Â
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 :
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 quoteourpath
. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
â Hunter
11 mins ago
add a comment |Â
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 :
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 quoteourpath
. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
â Hunter
11 mins ago
add a comment |Â
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 :
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 quoteourpath
. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
â Hunter
11 mins ago
add a comment |Â
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 :
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 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 quoteourpath
. I wonder if that is a mistake on their behave or if I'm doing something else wrong.
â Hunter
11 mins ago
add a comment |Â
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 quoteourpath
. 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
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