Lewy
Lewy

Reputation: 615

Does $PATH need subdirectories

Given a $PATH like:

/Users/myname/bin:
/usr/local/Cellar:
/usr/local/Cellar/ruby/1.9.3-p362/bin:
... and so forth

do I really need the ruby location or is /usr/local/Cellar sufficient to find ruby?

Upvotes: 3

Views: 1166

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

Basically, when you type a plain command name and it isn't a function or alias, then the shell logically takes the name and tries each directory in PATH in turn. In pseudo-code:

name=command_to_execute
for path in ${names in PATH}
    exec $path/$name "${arguments[@]}"
report "failed to find command $name"

If the exec succeeds, there's nothing more to do. If the exec fails, it tries the next directory.

Note that path search never applies to names containing a slash. And the shell only searches in exactly the directories listed in PATH (but empty entries are interpreted as references to ., the current directory). It won't look in sub-directories of its own volition; you would have to list the sub-directories in PATH.

Upvotes: 1

Eric Jablow
Eric Jablow

Reputation: 7889

The entries are independent of each other, and only count the direct contents. Try it.

> mkdir ~/bin
> mkdir ~/bin/test
> cp /bin/echo ~/bin/test/echotest
> export PATH=$PATH:~/bin
> echotest Hello, world
-bash: echotest: command not found
> rm -rf ~/bin/test

Upvotes: 5

Related Questions