Looping through the elements of a path variable in Bash

Viewed 20423

I want to loop through a path list that I have gotten from an echo $VARIABLE command.

For example:

echo $MANPATH will return

/usr/lib:/usr/sfw/lib:/usr/info

So that is three different paths, each separated by a colon. I want to loop though each of those paths. Is there a way to do that? Thanks.

Thanks for all the replies so far, it looks like I actually don't need a loop after all. I just need a way to take out the colon so I can run one ls command on those three paths.

10 Answers

OP's update wants to ls the resulting folders, and has pointed out that ls only requires a space-separated list.

ls $(echo $PATH | tr ':' ' ') is nice and simple and should fit the bill nicely.

Related