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 "%s\n" "$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?
type -a? – Stéphane Chazelas Apr 15 '16 at 03:41whichscript for how to loop over$PATHproperly. – Stéphane Chazelas Apr 15 '16 at 03:44IFS=':' ; 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? – Greg Nisbet Apr 15 '16 at 04:44whilethat'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 04:49