How to run the own program without specifying its path

command lineexecutablepath

Let's suppose I have compiled something and I run it like so:

$ /path/to/my/executable/mycmd
Hello World

What do I need to do to run it like

$ mycmd
Hello World 

from everywhere in my computer?

Best Answer

What you are looking for is the PATH environmental variable. It tells the shell, where it needs to look for programs. You can see the current value of that variable using echo:

echo "$PATH"

Now... The best practice if you want use some new program is to install it using the package management program for your distribution. But in this case, I assume you are dealing with a program that is not delivered by any available software package. For such programs, you have two options:

  1. Install the program system-wide, in a place where your system does not put any files installed from packages. On most systems, such "safe" folders include /usr/local/bin/ and /opt/bin/ - those should already be in your PATH. (Look inside these folders and if there are many files in them, then it is the wrong place to put your own program and you have to look at other folders listed in your PATH.)
  2. Modify your PATH variable. This is less secure, because it defines additional folders where programs can be kept and someone might play a trick on you, putting his own program there for you to run.

    You can modify the PATH variable either temporarily, using

    export PATH="$PATH:/path/to/your/executable"
    

    (mind the $PATH after =), or permanently by adding the above line to your .bashrc file (assuming you use bash).

Related Question