Search code examples
pythonbashpathdirectory-structureproject-structure

Executables in subfolder in path


I wish to place my executables in subfolders:

    $ tree
    .
    ├── bin
    │   ├── exec.py
    │   ├── method1
    │   │   ├── exec1.py
    │   │   └── exec2.py
    │   └── method2
    │       ├── exec1.py
    │       └── exec2.py

If I include bin/ folder in PATH, is there a way to access executables from my shell script (which is in another directory), as e.g.

$ method1/exec1.py arg1 arg2

The above command gives an error (No such file or directory), but

$ exec.py arg1 arg2

works, as it is not in a subfolder.

Kindly advise how I can organise executable files in subfolders. Thanks.


Solution

  • As documented here, the bash shell (and probably most other shells too) only looks for the command on the $PATH if the command name contains no slashes.

    There are several ways you can solve the issue:

    1. You can make a folder containing symbolic links to the commands and add that folder to the $PATH.
    2. You can add the subfolders to the $PATH. If there are many subfolders you can do it automatically
    3. You can change the scripts current directory to your bin. An easy way to temporarily change the current directory is to use subshells.
    4. And of course you can try using a different shell that does the command search differently. Sorry, but I don't know where to find one, you'll have to google this yourself if you really need it.