Add Multiple Subdirectories to PATH Under Same Parent Directory

path

I have installed some tools and put it under $HOME/tools/ and each tool has its own /bin directory that contains the executable program. I now have the path to each individual /bin in my $HOME/.bashrc file like this:

export PATH=$PATH:$HOME/tools/tool1/bin
export PATH=$PATH:$HOME/tools/tool2/bin
...

I'm wondering if it's OK to write the following for all the tools?

export PATH=$PATH:$HOME/tools

If it doesn't work, what is the best way to do this?

Best Answer

Adding a directory to PATH only makes the executables in this directory available as bare command names. This does not extend to subdirectories.

You can put a loop in your .profile (not .bashrc) to add multiple directories, for example:

for d in ~/tools/*/bin; do
  PATH="$PATH:$d"
done

(You don't need to repeat export: when a variable is exported, it stays exported, and modifications are reflected in the environment).

If you create new directories under tools, this will not be reflected in your ongoing session, only the next time .profile is read. If this is not satisfactory, you can use a different approach: put a single directory such as ~/bin in your PATH, and when you install software, create symbolic links in ~/bin. Stow (or its alternative XStow) is a good way of doing this; see Keeping track of programs for an overview of how this works.

Related Question