0

I am trying to add a path of a program/file into $PATH, but I don't understand the guidelines and finally, it doesn't work either. E.g. following this guideline:

  • if export PATH=$PATH:[path/to/program] is just a temporary solution, why do it for repeatable usage?
  • if I add export function for the path to bashrc and update it, it doesn't work either. And I suspect that it is because we haven't determined the name of the actual file? On the other hand, the error message mentions command: Command "foo" not found... so it takes me to the question, how the system determines between commands and programs?
Juandev
  • 101
  • 4
    Did you source ~/.bashrc after updating it, what does echo $PATH output, and, finally, where is this program located? You should add the directory where the program is located to PATH, not the complete path to the script/binary. – frippe Aug 17 '22 at 15:28
  • Yes I did source ~/.bashrc. echo $PATH shows me pathes in the $PATH so the path I have just added is also there: /juan/diskD/Programy/OpenRefine/openrefine-3.6.0. The program is located in the openrefine-3.6.0 directory. – Juandev Aug 17 '22 at 16:39
  • Is the program file's executable bit set? do the mount options on /juan/diskD permit program execution? – steeldriver Aug 17 '22 at 16:43
  • ls shows green color, so I expect its executable. Properties read shell script. I don't know what are mount options on a specific directory, but I can initiate the program from its mother directory via chmod a+x *program* and then /.*program*. – Juandev Aug 17 '22 at 16:52
  • The PATH variable should contain only directory names, so /path/to/program should be /path/to/directory/where/program/is/located/. – mook765 Aug 17 '22 at 17:47
  • Well, I have there just directories. – Juandev Aug 23 '22 at 08:18

2 Answers2

0

if export PATH=$PATH:[path/to/program] is just a temporary solution, why do it for repeatable usage?

Like you say, it is a temporary solution for use in only a single session.

if I add export function for the path to bashrc and update it, it doesn't work either. And I suspect that it is because we haven't determined the name of the actual file?

.bashrc is run each time you open an active terminal. If correctly entered in a location of the script that is run, it should work. If you prefer the entry to work always, e.g. also from a script run from a program laucher, or from the Alt+F2 run dialog, add the statement to .profile instead. That file is read when you log in.

No, your suspicion is wrong. One a (valid) directory is added to PATH, the system will search that directory for a file name matching a command you entered at the terminal prompt.

so it takes me to the question, how the system determines between commands and programs?

A command is a string you type in the terminal to execute something. It can be a shell builtin, or it can be an executable file. An executable file is a file where the executable bit has been set. It can be a script (bash, python, ...) or binary code. It may be a matter of semantics, but any command corresponds to some programmed code, be it a shell builtin, e.g. cd or an executable file to launch, e.g. gedit.

You can specify a full pathname to an executable. Then the shell will immediately locate it and (try to) execute it.

If you just type a name, the shell will start searching for something with that name to execute:

  1. It checks whether the name corresponds with an internal command, i.e. a "shell builtin". A shell builtin resides in memory as part of your command interpreter, typically bash in interactive shells on Ubuntu. cd, pwd, echo and set are examples of shell builtins (in bash).

  2. Then it goes looking into the hash table, a trick to speed up finding executables. When you just logged in, the hash table is empty.

  3. Then it proceeds inspecting each folder in your PATH, in the order folders are listed. So if the path is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/games, /usr/local/sbin is searched for the string. If it is not found there, the system continues searching in he next directory, /usr/local/bin etc. ls and chmod are example of executables.

You can learn about your commands with the type command. For example:

$ type man
man is hashed (/usr/bin/man)
$ type ls
ls is aliased to `ls --color=auto'
$ type chmod
chmod is /usr/bin/chmod
$ type pwd
pwd is a shell builtin

You can identify and locate external executables with the which command:

$ which cd
$ which ls
/usr/bin/ls
vanadium
  • 88,010
  • If you add a remark that .bashrc is only read for interactive non-login shells and suggest /etc/profile.d/ over /etc/profile, you have my upvote :) – frippe Aug 18 '22 at 18:59
  • The scope of the question is user account configuration, and I am already saying in non technical language that .bashrc is read each time you open a terminal. – vanadium Aug 19 '22 at 06:33
0

You don't probably need to change your PATH. It is usually good as is. Consider one of other possibilities:

  1. Moving the program to one of the directories that are already in PATH, particularly /home/UserName/.local/bin.

  2. Creating an alias for the program in your shell, so you can execute the program simply by typing it's nice alias.

    gedit ~/.bash_aliases
    

    Add a line like:

    alias do-it='/path/to/program/program-name'
    

    Restart the shell or do source ~/.bashrc. Execute using alias:

    do-it
    

    It works as if you've called the program directly:

    /path/to/program/program-name