I need to loop over all directories in $PATH variable. Something like this:
for directory in "$PATH"; do
echo $directory
done
So if my $PATH variable is /usr/bin /bin /usr/sbin /sbin /usr/local/bin
I need to have a loop with five directories: /usr/bin
, /bin
, /usr/sbin
, /sbin
, /usr/local/bin
, /usr/local/go/bin
How can I do it ?
I think the best approach is this:
which uses the
read
command to read "lines", with:
being the line terminator. (SettingIFS
to the empty string ensures thatread
won't try to split the line into words, in case your path contains any whitespace or whatnot.)