Search code examples
linuxunixpathbin

Running custom program located in /bin


I have created a folder ~/bin. This is my own bin to store my own custom programs. As a test, I wrote a simple hello world program. I export using PATH=~/bin:$PATH so that Linux looks in that directory. If I save hello (my test program name) I can successfully run it from the command line by typing hello, similar to if I were running ls, grep, etc. I would like however to include in my path a way to search all subdirectories of bin. Ie. if I created a couple programs, lets just call them leapyear and hello to make life easy, and each was in its own subdirectory, can I add something generic to the $PATH environmental variable to recognize both programs? Now, I am more than aware that hello and leapyear are not really system utilities, but it works for illustrating my goal. I have tried /* to include all subdirectories and that didn't seem to work out.


Solution

  • You can do it through your .bashrc, for example, like this:

    PATH=${PATH}:$(find ~/bin -type d | tr '\n' ':' | sed 's/:$//')
    

    Explanation:

    • we search only for directories
    • newlines get separated with :
    • last : is stripped

    Aside from solutions like this, PATH doesn't understand pretty much anything other than :.