5

Is it possible to recursively add a directory to my $PATH variable? Let's say I have a directory structure like the following:

/usr/local/bin
    - /nodejs-x.x
    - /redis-x.x
    - /mongodb-x-x

Can I add /usr/local/bin to the $PATH and allow it to recursively cover the subdirectories?

1 Answer 1

6

Not directly, no. Entries in $PATH are not recursive.

What you can do is:

for d in /usr/local/bin/*/; do
    PATH+=":$d"
done

Another option is to put symlinks in /usr/local/bin:

cd /usr/local/bin
ln -s myapp-1.2/myapp myapp
3
  • 1
    Alright, makes sense. Is there any reasonable limit on the length of the $PATH variable? I guess it can be as long as you want but is there some drawback to having it super long?
    – ajackbot
    Oct 11, 2011 at 13:06
  • @jackweeden: The environment block is limited to 1/4 of the allowed stack space (as shown by ulimit -a; 2 MB by default), and separate strings (key=value pairs) are limited to 32 memory pages (32 kB on a 4-kB-pages architecture). Oct 11, 2011 at 13:32
  • @jackweeden: A minor drawback is that all directories will have to be checked, one by one, for every command you run. Even though bash remembers the results after first use, it doesn't share this information among instances and forgets after exiting. With modern storage media this isn't much of an issue, though. Oct 11, 2011 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .