help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to show the path of a bash script in $PATH?


From: Greg Wooledge
Subject: Re: [Help-bash] How to show the path of a bash script in $PATH?
Date: Mon, 6 Apr 2015 08:25:57 -0400
User-agent: Mutt/1.4.2.3i

On Sun, Apr 05, 2015 at 09:57:59AM -0500, Peng Yu wrote:
> `which` only shows the path if the command is an executable.

which(1) is a nonstandard, unreliable external program.  Don't use it
for any serious work.  Use "type" instead.

> What if a
> bash script is in $PATH but it is just for `source` only (i.e., not
> executable)? Is there an easy to find its path?

So what you want to know is "what file would bash load if I were to
issue the command 'source foo'".

Why do you want to know this?  Morbid curiosity?  Are you debugging
some kind of problem?  If so, what problem?

> This can be done with `find` and a bit programming.

I don't see why you would use find(1).  Just iterate through the
paths in $PATH and do a bunch of [[ -r ]] checks.

> But since bash is
> able to resolve the path of a script for sourcing, I'd imagine there
> might be a way to use this bash capability to directly get the path of
> the script.

Perhaps, but if so, it's not obvious how to do it.  Usually when it's
not obvious how to do something, it's because nobody else has ever
wanted to do that.

I think the fundamental disconnect here is that everyone else but you
is appalled by the fact that source (or .) searches $PATH.  Most people
give an explicit path to the file they want to source (e.g.
source ~/.bash_aliases or source /etc/default/myapp) to avoid surprises.

If you've got a file that is not intended to be run standalone, why are
you keeping it in a directory that's in PATH, instead of in a specific
place for script-libraries?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]