1

Context: macOS Catalina zSh. The end goal is to be able to setup user scripts so that they can be invoked via CLI at an arbitrary data location.

Question: What is the word (Terminology) for: the terminal command to be invoked from any directory? The correct word / phrase would allow me to Google the subject matter and inch forward to the goal defined below.

Example: the command ls can be issued from any directory and return output. Likewise when lsusb was installed with homebrew: lsusb will work from any directory: somehow when lsusb was installed, the system was configured such that it knows where to look to find lsusb.

Path Variable: I understand that the $PATH variable contains path information:

babygrand:~ user$ echo $PATH

returns

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I suspect that /etc/paths is configures $PATH at startup:

babygrand:~ user$ cat /etc/paths

/usr/local/bin /usr/bin /bin /usr/sbin /sbin

The output paths seem to be a variation of the File System Hierarchy, which poses a similar question to my goal, but not my Terminology question

GOAL: I would like to Google the word / term of the subject matter and learn best practice how to enable a script to be run from any directory. I am faced with a decision as to whether to add another path or to choose an one of the above existing paths that has been already configured into the $PATH variable.

The ability to invoke my script at an arbitrary directory is of significant utility: the script performs work on pdf files, which could be located anywhere in the file system. My script calls an executable to stamp pdf documents.

Clarification: The subject matter is not that of the "subfolder" problem posed at: https://apple.stackexchange.com/q/366127/187540 This is excellent reading as it is somewhat related to directory management.

8
  • "the terminal command to be invoked…" the terminal command that can be invoked from any directory and produce an identical result irrespective of the pwd (like your lsusb) or even if producing a result depending on the pwd like your ls command ?
    – MC68020
    Commented Jun 12, 2022 at 15:59
  • Thanks for the question: No, the output would be different if invoked from a different directory. The issue is whether the user script can be invoked from a arbitrary data directory. Currently my script can not be invoked from a directory outside of the script's directory. I would like to understand the word / phrase that captures the idea of being able to invoke the script from any directory.
    – gatorback
    Commented Jun 12, 2022 at 16:04
  • Ha ! Too bad. In the first case (producing a result irrespective of the pwd) and from a stricly linguistic stand point (irrespective of what google would like to index), I would have suggested folder-agnostic or directory-agnostic.
    – MC68020
    Commented Jun 12, 2022 at 16:08
  • 1
    @MC68020 I cut my teeth on the 68HC11: really cool tech back in the day!
    – gatorback
    Commented Jun 12, 2022 at 16:11
  • 1
    OK Google ;-) ! folder-agnostic actually provides some related answers on google : apple.stackexchange.com/questions/366127/…
    – MC68020
    Commented Jun 12, 2022 at 16:16

1 Answer 1

2

As far as I know, there is no specific well-known term for what you seem to be asking, other than just saying that the application/utility/script is or should be placed in PATH, as a shorthand for "in a directory included in executable search path". Unfortunately the short form is not very googleable and the long form is rarely used.

If you intend your script to be available for all users, the "standard" way would be to add (or link) it to /usr/local/bin, which would be the expected location for scripts and tools managed for the local system administrator and intended to be usable for all users of the system. This directory is normally included in the system default $PATH for users.

Note that in your example, /usr/local/bin is listed in $PATH before the /usr/bin and /bin directories of the system standard tools. This allows the system administrator to override the standard tools by placing e.g. a newer version of some tool to /usr/local/bin using the exact same name as the corresponding standard system tool.

If the script was intended for all system administrators only, then respectively /usr/local/sbin would be the expected location. However this is not always automatically added to the default $PATH for system administrators.

But if you want to set it up for yourself only, the easiest way would be to set up a directory for your scripts (e.g. $HOME/bin, although you could name it anything you wish), and add it to the system default $PATH in your personal login script.

If you modify the default $PATH, you can either completely replace the existing value (not recommended unless you know what you're doing), or alternatively prepend or append your custom script directory to $PATH.

Appending $HOME/bin to the current $PATH:

export PATH="$PATH:$HOME/bin"

By appending your custom directory, the scripts within it will be placed last in the search order, guaranteeing that you won't accidentally cause problems to yourself by naming your script the same as some standard system tool.

Conversely, if you want the ability to override the standard tools with your own, you can prepend your script/tool directory to $PATH:

export PATH="$HOME/bin:$PATH"

To make this change permanent so it affects al shell sessions and survives rebooting, just add the line above to your ~/.profile file (you can create it if it doesn't exist already).

0

You must log in to answer this question.

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