Bash while loop read from colon-delimited list of paths using IFSWhy not use “which”? What to use then?Why is using a shell loop to process text considered bad practice?Understanding “IFS= read -r line”Bash: why isn't “set” behaving like I expect it to?Reading lines from a file with bash: for vs. whileHow given script makes executables in /usr/local/bin/$PATH duplication issuesRead longest line in file using while read loop?How to prevent the caller's shell from being used in sudobash while/read loop behaves differently in a mips/musl/busybox based VMBash script works via terminal but not via main menuHow to loop over ever-increasing list of files in bash?what shell is used to run a script
Why is this clock signal connected to a capacitor to gnd?
Short story with a alien planet, government officials must wear exploding medallions
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Should I cover my bicycle overnight while bikepacking?
What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?
Method Does Not Exist error message
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
What type of content (depth/breadth) is expected for a short presentation for Asst Professor interview in the UK?
iPad being using in wall mount battery swollen
Can compressed videos be decoded back to their uncompresed original format?
Apex Framework / library for consuming REST services
Why can't we play rap on piano?
How could indestructible materials be used in power generation?
How does a predictive coding aid in lossless compression?
Is it inappropriate for a student to attend their mentor's dissertation defense?
How to tell a function to use the default argument values?
How dangerous is XSS?
Bullying boss launched a smear campaign and made me unemployable
How to prevent "they're falling in love" trope
How can saying a song's name be a copyright violation?
Why do bosons tend to occupy the same state?
Why is consensus so controversial in Britain?
What exploit Are these user agents trying to use?
In 'Revenger,' what does 'cove' come from?
Bash while loop read from colon-delimited list of paths using IFS
Why not use “which”? What to use then?Why is using a shell loop to process text considered bad practice?Understanding “IFS= read -r line”Bash: why isn't “set” behaving like I expect it to?Reading lines from a file with bash: for vs. whileHow given script makes executables in /usr/local/bin/$PATH duplication issuesRead longest line in file using while read loop?How to prevent the caller's shell from being used in sudobash while/read loop behaves differently in a mips/musl/busybox based VMBash script works via terminal but not via main menuHow to loop over ever-increasing list of files in bash?what shell is used to run a script
I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given name, even if they are shadowed, e.g.
tcsh> where tcsh
/usr/bin/tcsh
/bin/tcsh
As part of this I want to loop over everything in the $PATH and see if an executable file with the appropriate name exists.
The following bash snippet is intended to loop over a colon-delimited list of paths and print each component followed by a newline, however, it just seems to print the entire contents of $PATH all on one line
#!/bin/bash
while IFS=':' read -r line; do
printf "%sn" "$line"
done <<< "$PATH"
As is stands now, bash where and ./where just print /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So, how do I set up my while loop so that the value of the loop variable is each segment of the colon-separated list of paths in turn?
bash shell string
add a comment |
I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given name, even if they are shadowed, e.g.
tcsh> where tcsh
/usr/bin/tcsh
/bin/tcsh
As part of this I want to loop over everything in the $PATH and see if an executable file with the appropriate name exists.
The following bash snippet is intended to loop over a colon-delimited list of paths and print each component followed by a newline, however, it just seems to print the entire contents of $PATH all on one line
#!/bin/bash
while IFS=':' read -r line; do
printf "%sn" "$line"
done <<< "$PATH"
As is stands now, bash where and ./where just print /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So, how do I set up my while loop so that the value of the loop variable is each segment of the colon-separated list of paths in turn?
bash shell string
6
Why not usetype -a?
– Stéphane Chazelas
Apr 15 '16 at 3:41
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
2
See also Debian'swhichscript for how to loop over$PATHproperly.
– Stéphane Chazelas
Apr 15 '16 at 3:44
@StéphaneChazelas I see so if you useIFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of$PATHin turn. Why do theforandwhileloop treatIFSdifferently?
– Gregory Nisbet
Apr 15 '16 at 4:44
2
@GregoryNisbet it's notwhilethat's doing anything withIFSin this case, it'sread, which reads a whole line at once, regardless ofIFS—as Stephane explained, actually.
– Wildcard
Apr 15 '16 at 4:49
add a comment |
I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given name, even if they are shadowed, e.g.
tcsh> where tcsh
/usr/bin/tcsh
/bin/tcsh
As part of this I want to loop over everything in the $PATH and see if an executable file with the appropriate name exists.
The following bash snippet is intended to loop over a colon-delimited list of paths and print each component followed by a newline, however, it just seems to print the entire contents of $PATH all on one line
#!/bin/bash
while IFS=':' read -r line; do
printf "%sn" "$line"
done <<< "$PATH"
As is stands now, bash where and ./where just print /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So, how do I set up my while loop so that the value of the loop variable is each segment of the colon-separated list of paths in turn?
bash shell string
I am trying to write a bash function that behaves similarly to the where builtin in tcsh. In tcsh, where lists all the builtins, aliases, and the absolute paths to executables on the PATH with a given name, even if they are shadowed, e.g.
tcsh> where tcsh
/usr/bin/tcsh
/bin/tcsh
As part of this I want to loop over everything in the $PATH and see if an executable file with the appropriate name exists.
The following bash snippet is intended to loop over a colon-delimited list of paths and print each component followed by a newline, however, it just seems to print the entire contents of $PATH all on one line
#!/bin/bash
while IFS=':' read -r line; do
printf "%sn" "$line"
done <<< "$PATH"
As is stands now, bash where and ./where just print /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So, how do I set up my while loop so that the value of the loop variable is each segment of the colon-separated list of paths in turn?
bash shell string
bash shell string
edited Jun 28 '17 at 14:19
Gregory Nisbet
asked Apr 15 '16 at 3:25
Gregory NisbetGregory Nisbet
1,4981020
1,4981020
6
Why not usetype -a?
– Stéphane Chazelas
Apr 15 '16 at 3:41
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
2
See also Debian'swhichscript for how to loop over$PATHproperly.
– Stéphane Chazelas
Apr 15 '16 at 3:44
@StéphaneChazelas I see so if you useIFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of$PATHin turn. Why do theforandwhileloop treatIFSdifferently?
– Gregory Nisbet
Apr 15 '16 at 4:44
2
@GregoryNisbet it's notwhilethat's doing anything withIFSin this case, it'sread, which reads a whole line at once, regardless ofIFS—as Stephane explained, actually.
– Wildcard
Apr 15 '16 at 4:49
add a comment |
6
Why not usetype -a?
– Stéphane Chazelas
Apr 15 '16 at 3:41
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
2
See also Debian'swhichscript for how to loop over$PATHproperly.
– Stéphane Chazelas
Apr 15 '16 at 3:44
@StéphaneChazelas I see so if you useIFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of$PATHin turn. Why do theforandwhileloop treatIFSdifferently?
– Gregory Nisbet
Apr 15 '16 at 4:44
2
@GregoryNisbet it's notwhilethat's doing anything withIFSin this case, it'sread, which reads a whole line at once, regardless ofIFS—as Stephane explained, actually.
– Wildcard
Apr 15 '16 at 4:49
6
6
Why not use
type -a?– Stéphane Chazelas
Apr 15 '16 at 3:41
Why not use
type -a?– Stéphane Chazelas
Apr 15 '16 at 3:41
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
2
2
See also Debian's
which script for how to loop over $PATH properly.– Stéphane Chazelas
Apr 15 '16 at 3:44
See also Debian's
which script for how to loop over $PATH properly.– Stéphane Chazelas
Apr 15 '16 at 3:44
@StéphaneChazelas I see so if you use
IFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of $PATH in turn. Why do the for and while loop treat IFS differently?– Gregory Nisbet
Apr 15 '16 at 4:44
@StéphaneChazelas I see so if you use
IFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of $PATH in turn. Why do the for and while loop treat IFS differently?– Gregory Nisbet
Apr 15 '16 at 4:44
2
2
@GregoryNisbet it's not
while that's doing anything with IFS in this case, it's read, which reads a whole line at once, regardless of IFS—as Stephane explained, actually.– Wildcard
Apr 15 '16 at 4:49
@GregoryNisbet it's not
while that's doing anything with IFS in this case, it's read, which reads a whole line at once, regardless of IFS—as Stephane explained, actually.– Wildcard
Apr 15 '16 at 4:49
add a comment |
5 Answers
5
active
oldest
votes
read uses IFS to separate the words in the line it reads, it doesn't tell read to read until the first occurrence of any of the characters in it.
IFS=: read -r a b
Would read one line, put the part before the first : in $a, and the rest in $b.
IFS=: read -r a
would put the whole line (the rest) in $a (except if that line contains only one : and it's the last character on the line).
If you wanted to read until the first :, you'd use read -d: instead (ksh93, zsh or bash only).
printf %s "$PATH" | while IFS= read -rd: dir || [ -n "$dir" ]; do
...
done
(we're not using <<< as that adds an extra newline character).
Or you could use standard word splitting:
IFS=:; set -o noglob
for dir in $PATH""; do
...
done
Now beware of few caveats:
- An empty
$PATHcomponent means the current directory. - An empty
$PATHmeans the current directory (that is,$PATHcontains one component which is the current directory, so thewhile read -d:loop would be wrong in that case). //fileis not necessary the same as/fileon some system, so if$PATHcontains/, you need to be careful with things like$dir/$file.- An unset $PATH means a default search path is to be used, it's not the same as a set but empty $PATH.
Now, if it's only the equivalent of tcsh/zsh's where command, you could use bash's type -a.
More reading:
- What's a safe and portable way to split a string in shell programming?
- Understanding "IFS= read -r line"
- Why not use "which"? What to use then?
add a comment |
Don't use shell loops to process text.
Instead, use awk, or tr, or even sed.
printf %s\n "$PATH" | tr ':' 'n'
printf %s "$PATH" | awk 'BEGIN RS=":"; 1'
Or, since this is a shell variable you are processing, just use bash pattern substitution:
echo "$PATH//:/
"
(See LESS=+/parameter/pattern man bash.)
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
add a comment |
This script works similar to the command where
#!/bin/bash
fn="foo"
for i in `echo $PATH|tr ':' 'n'`
do
if [[ -e "$i/$fn" ]] ; then
echo $i
fi
done
$fn is something you want to find.
$i/$fn may be $i$fn sometimes.
add a comment |
Pure bash, it's the same thing as Wildcard's 2nd method, but this is on one line:
echo -e "$PATH//:/"n""
add a comment |
Shell utils:
echo $PATH | tr ':' 'n'
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f276614%2fbash-while-loop-read-from-colon-delimited-list-of-paths-using-ifs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
read uses IFS to separate the words in the line it reads, it doesn't tell read to read until the first occurrence of any of the characters in it.
IFS=: read -r a b
Would read one line, put the part before the first : in $a, and the rest in $b.
IFS=: read -r a
would put the whole line (the rest) in $a (except if that line contains only one : and it's the last character on the line).
If you wanted to read until the first :, you'd use read -d: instead (ksh93, zsh or bash only).
printf %s "$PATH" | while IFS= read -rd: dir || [ -n "$dir" ]; do
...
done
(we're not using <<< as that adds an extra newline character).
Or you could use standard word splitting:
IFS=:; set -o noglob
for dir in $PATH""; do
...
done
Now beware of few caveats:
- An empty
$PATHcomponent means the current directory. - An empty
$PATHmeans the current directory (that is,$PATHcontains one component which is the current directory, so thewhile read -d:loop would be wrong in that case). //fileis not necessary the same as/fileon some system, so if$PATHcontains/, you need to be careful with things like$dir/$file.- An unset $PATH means a default search path is to be used, it's not the same as a set but empty $PATH.
Now, if it's only the equivalent of tcsh/zsh's where command, you could use bash's type -a.
More reading:
- What's a safe and portable way to split a string in shell programming?
- Understanding "IFS= read -r line"
- Why not use "which"? What to use then?
add a comment |
read uses IFS to separate the words in the line it reads, it doesn't tell read to read until the first occurrence of any of the characters in it.
IFS=: read -r a b
Would read one line, put the part before the first : in $a, and the rest in $b.
IFS=: read -r a
would put the whole line (the rest) in $a (except if that line contains only one : and it's the last character on the line).
If you wanted to read until the first :, you'd use read -d: instead (ksh93, zsh or bash only).
printf %s "$PATH" | while IFS= read -rd: dir || [ -n "$dir" ]; do
...
done
(we're not using <<< as that adds an extra newline character).
Or you could use standard word splitting:
IFS=:; set -o noglob
for dir in $PATH""; do
...
done
Now beware of few caveats:
- An empty
$PATHcomponent means the current directory. - An empty
$PATHmeans the current directory (that is,$PATHcontains one component which is the current directory, so thewhile read -d:loop would be wrong in that case). //fileis not necessary the same as/fileon some system, so if$PATHcontains/, you need to be careful with things like$dir/$file.- An unset $PATH means a default search path is to be used, it's not the same as a set but empty $PATH.
Now, if it's only the equivalent of tcsh/zsh's where command, you could use bash's type -a.
More reading:
- What's a safe and portable way to split a string in shell programming?
- Understanding "IFS= read -r line"
- Why not use "which"? What to use then?
add a comment |
read uses IFS to separate the words in the line it reads, it doesn't tell read to read until the first occurrence of any of the characters in it.
IFS=: read -r a b
Would read one line, put the part before the first : in $a, and the rest in $b.
IFS=: read -r a
would put the whole line (the rest) in $a (except if that line contains only one : and it's the last character on the line).
If you wanted to read until the first :, you'd use read -d: instead (ksh93, zsh or bash only).
printf %s "$PATH" | while IFS= read -rd: dir || [ -n "$dir" ]; do
...
done
(we're not using <<< as that adds an extra newline character).
Or you could use standard word splitting:
IFS=:; set -o noglob
for dir in $PATH""; do
...
done
Now beware of few caveats:
- An empty
$PATHcomponent means the current directory. - An empty
$PATHmeans the current directory (that is,$PATHcontains one component which is the current directory, so thewhile read -d:loop would be wrong in that case). //fileis not necessary the same as/fileon some system, so if$PATHcontains/, you need to be careful with things like$dir/$file.- An unset $PATH means a default search path is to be used, it's not the same as a set but empty $PATH.
Now, if it's only the equivalent of tcsh/zsh's where command, you could use bash's type -a.
More reading:
- What's a safe and portable way to split a string in shell programming?
- Understanding "IFS= read -r line"
- Why not use "which"? What to use then?
read uses IFS to separate the words in the line it reads, it doesn't tell read to read until the first occurrence of any of the characters in it.
IFS=: read -r a b
Would read one line, put the part before the first : in $a, and the rest in $b.
IFS=: read -r a
would put the whole line (the rest) in $a (except if that line contains only one : and it's the last character on the line).
If you wanted to read until the first :, you'd use read -d: instead (ksh93, zsh or bash only).
printf %s "$PATH" | while IFS= read -rd: dir || [ -n "$dir" ]; do
...
done
(we're not using <<< as that adds an extra newline character).
Or you could use standard word splitting:
IFS=:; set -o noglob
for dir in $PATH""; do
...
done
Now beware of few caveats:
- An empty
$PATHcomponent means the current directory. - An empty
$PATHmeans the current directory (that is,$PATHcontains one component which is the current directory, so thewhile read -d:loop would be wrong in that case). //fileis not necessary the same as/fileon some system, so if$PATHcontains/, you need to be careful with things like$dir/$file.- An unset $PATH means a default search path is to be used, it's not the same as a set but empty $PATH.
Now, if it's only the equivalent of tcsh/zsh's where command, you could use bash's type -a.
More reading:
- What's a safe and portable way to split a string in shell programming?
- Understanding "IFS= read -r line"
- Why not use "which"? What to use then?
edited Sep 11 '18 at 16:26
answered Apr 15 '16 at 4:00
Stéphane ChazelasStéphane Chazelas
312k57592948
312k57592948
add a comment |
add a comment |
Don't use shell loops to process text.
Instead, use awk, or tr, or even sed.
printf %s\n "$PATH" | tr ':' 'n'
printf %s "$PATH" | awk 'BEGIN RS=":"; 1'
Or, since this is a shell variable you are processing, just use bash pattern substitution:
echo "$PATH//:/
"
(See LESS=+/parameter/pattern man bash.)
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
add a comment |
Don't use shell loops to process text.
Instead, use awk, or tr, or even sed.
printf %s\n "$PATH" | tr ':' 'n'
printf %s "$PATH" | awk 'BEGIN RS=":"; 1'
Or, since this is a shell variable you are processing, just use bash pattern substitution:
echo "$PATH//:/
"
(See LESS=+/parameter/pattern man bash.)
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
add a comment |
Don't use shell loops to process text.
Instead, use awk, or tr, or even sed.
printf %s\n "$PATH" | tr ':' 'n'
printf %s "$PATH" | awk 'BEGIN RS=":"; 1'
Or, since this is a shell variable you are processing, just use bash pattern substitution:
echo "$PATH//:/
"
(See LESS=+/parameter/pattern man bash.)
Don't use shell loops to process text.
Instead, use awk, or tr, or even sed.
printf %s\n "$PATH" | tr ':' 'n'
printf %s "$PATH" | awk 'BEGIN RS=":"; 1'
Or, since this is a shell variable you are processing, just use bash pattern substitution:
echo "$PATH//:/
"
(See LESS=+/parameter/pattern man bash.)
edited Apr 13 '17 at 12:37
Community♦
1
1
answered Apr 15 '16 at 3:39
WildcardWildcard
23.3k1067171
23.3k1067171
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
add a comment |
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
2
2
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
Why the downvote? I see three other answers have been given downvotes just now with no comments...why?
– Wildcard
Apr 15 '16 at 19:34
add a comment |
This script works similar to the command where
#!/bin/bash
fn="foo"
for i in `echo $PATH|tr ':' 'n'`
do
if [[ -e "$i/$fn" ]] ; then
echo $i
fi
done
$fn is something you want to find.
$i/$fn may be $i$fn sometimes.
add a comment |
This script works similar to the command where
#!/bin/bash
fn="foo"
for i in `echo $PATH|tr ':' 'n'`
do
if [[ -e "$i/$fn" ]] ; then
echo $i
fi
done
$fn is something you want to find.
$i/$fn may be $i$fn sometimes.
add a comment |
This script works similar to the command where
#!/bin/bash
fn="foo"
for i in `echo $PATH|tr ':' 'n'`
do
if [[ -e "$i/$fn" ]] ; then
echo $i
fi
done
$fn is something you want to find.
$i/$fn may be $i$fn sometimes.
This script works similar to the command where
#!/bin/bash
fn="foo"
for i in `echo $PATH|tr ':' 'n'`
do
if [[ -e "$i/$fn" ]] ; then
echo $i
fi
done
$fn is something you want to find.
$i/$fn may be $i$fn sometimes.
edited Apr 15 '16 at 4:42
Lucas
2,033818
2,033818
answered Apr 15 '16 at 4:05
framsframs
383217
383217
add a comment |
add a comment |
Pure bash, it's the same thing as Wildcard's 2nd method, but this is on one line:
echo -e "$PATH//:/"n""
add a comment |
Pure bash, it's the same thing as Wildcard's 2nd method, but this is on one line:
echo -e "$PATH//:/"n""
add a comment |
Pure bash, it's the same thing as Wildcard's 2nd method, but this is on one line:
echo -e "$PATH//:/"n""
Pure bash, it's the same thing as Wildcard's 2nd method, but this is on one line:
echo -e "$PATH//:/"n""
edited 2 days ago
answered Apr 15 '16 at 4:05
agcagc
4,75711138
4,75711138
add a comment |
add a comment |
Shell utils:
echo $PATH | tr ':' 'n'
add a comment |
Shell utils:
echo $PATH | tr ':' 'n'
add a comment |
Shell utils:
echo $PATH | tr ':' 'n'
Shell utils:
echo $PATH | tr ':' 'n'
answered Apr 15 '16 at 3:44
agcagc
4,75711138
4,75711138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f276614%2fbash-while-loop-read-from-colon-delimited-list-of-paths-using-ifs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Why not use
type -a?– Stéphane Chazelas
Apr 15 '16 at 3:41
@StéphaneChazelas because I didn't know it existed. :/
– Gregory Nisbet
Apr 15 '16 at 3:43
2
See also Debian's
whichscript for how to loop over$PATHproperly.– Stéphane Chazelas
Apr 15 '16 at 3:44
@StéphaneChazelas I see so if you use
IFS=':' ; for x in $PATH ; do echo "$x" ; done, then the loop variable will be set to each element of$PATHin turn. Why do theforandwhileloop treatIFSdifferently?– Gregory Nisbet
Apr 15 '16 at 4:44
2
@GregoryNisbet it's not
whilethat's doing anything withIFSin this case, it'sread, which reads a whole line at once, regardless ofIFS—as Stephane explained, actually.– Wildcard
Apr 15 '16 at 4:49