help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Special PATH variable for sourcing scripts?


From: Matheus Afonso Martins Moreira
Subject: Special PATH variable for sourcing scripts?
Date: Sun, 21 Apr 2024 18:42:32 +0000

Hello! I've been building up a small library of bash functions
and would like to organize them properly by splitting them into
independently loadable scripts. Bash scripts can be hard to get
right so reusing proven solutions is of immense value.

Something like this:

    # Reusable terminal functions
    # Finds and sources ~/.local/lib/bash/terminal
    source terminal

    # Reusable argument parsing functions
    # Finds and sources ~/.local/lib/bash/arguments
    source arguments

However, the manual[1] reveals that bash uses
the PATH variable for resolving names when
sourcing scripts that way.

    . (a period)

        If filename does not contain a slash,
        the PATH variable is used to find filename

This is not ideal. It requires either:

 - Adding ~/.local/lib/bash to the PATH
       Incorrect since one does not want libraries
       in the PATH meant for actual executables.

 - Prefixing the full path to the library
       Makes code hard to write and even harder to read.

So it would be great if bash had a separate PATH variable
meant specifically for sourcing bash library scripts.
It could be called BASH_LIBRARY_PATH.

Bash already has a BASH_LOADABLES_PATH[2] variable:

    BASH_LOADABLES_PATH

        A colon-separated list of directories
        in which the shell looks for dynamically
        loadable builtins specified by the enable
        command.

The BASH_LIBRARY_PATH variable would be similar.
If it is defined, it disables the normal PATH
lookup behavior of the dot operator, making it
so that bash searches only the directories in
that list instead. Existing environments will
not contain that variable, compatibility is
therefore maintained.

Do you think this would be a good addition
to bash? Would a patch be accepted?
Please let me know if I should try.

Thanks,
  Matheus

References:

[1]: 
https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html
[2]: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]