Finding a script/program on $PATH

I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress:

 echo $PATH |sed "s/:/^M/g"

Any ideas? TIA

echo $PATH | sed -e 's/:/\n/g' | xargs <command>

You could try several ways. Here's one with tr:-

tr ":" " " <<-END | xargs ls -l 
$PATH
END

Something with the IFS variable is possible too, but what is your goal? That might give us a different approach entirely.

Robin

2 Likes

tr ":" "\n" did what I needed. sed didn't do anything with the backslash but ignore it, the letter "n" separated the path directories rather than ":". Thanks for looking at it.

You must have an ancient BSD-ish sed, many newer ones accept \n these days.

Perhaps I'm completely missing the point, however, why don't you just use "which"

> which grep
/usr/bin/grep

Let the system do the heavy lifting for you?
Now you have the full path to the command, just report it in whatever way you want ??

1 Like

And I have seen several "solutions" like this that were unnecessary.
Usually you set up your PATH first, then simply run the commands, and let the shell find them.

Wasn't familiar with the which command. Thanks.