13

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?

2 Answers 2

15

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).

0

User scripts (or compiled executables) are usually stored in the ~/bin directory. This allows you to run them without specifying a path.

But if you don't want to mix your own scripts with scripts placed in ~/bin by installed packages and programs, you can store your scripts in a directory of your choice and bind it to ~/bin. Your scripts will be visible in ~/bin and you will be able to run them from a terminal with a simple name without having to specify a path.

For one time use you can run this command (effect will disappear after reboot):

mkdir /home/user/.overlay
sudo mount -t overlay overlay -o lowerdir=/home/user/my-scripts-directory,upperdir=/home/user/bin,workdir=/home/user/.overlay /home/user/bin

You can create a new script with this command and run it automatically at login to remount this binding after a reboot.

Or

To bind permanently, add these lines to fstab:

# bind my scripts to ~/bin
overlay /home/user/bin overlay defaults,lowerdir=/home/user/my-scripts-directory,upperdir=/home/user/bin,workdir=/home/user/.overlay 0 0

The method below I have used earlier, but it has a flaw that I did not notice: the content of the real ~/bin folder become inaccessible. So I do not recommend to use it. I found another method that I described above, but I leave this old method here to avoid misunderstanding what we talked about in the comments.

For one time use you can run this command (effect will disappear after reboot):
sudo mount --bind ~/my-scripts-directory ~/bin
You can create a new script with this command and run it automatically at login to remount this binding after a reboot.
To bind permanently, add these lines to fstab:
# bind my scripts to ~/bin
/home/user/my-scripts-directory /home/user/bin none bind 0 0

9
  • (1) Not all distros put ~/bin in PATH by default, so even if you put them in ~/bin you might have to follow rozcietrzewiacz's answer anyway, and (2) why wouldn't you just use a symbolic link or alias rather than modifying fstab? Not everyone has root access.
    – frabjous
    Mar 19 at 14:58
  • @frabjous, and not everyone writes scripts and compiles programs. what do you suggest creating a symbolic link to? to one program or script? but if you have a bunch of scripts and you periodically create new ones, then this may require a lot of effort. Mar 19 at 15:20
  • @frabjous, anyway, I'm not saying how you SHOULD - I'm just suggesting how you CAN store your scripts. Mar 19 at 15:28
  • Folders can be targets of links. Your answer involved mounting the folder somewhere else. Why not create a symbolic link to the folder instead? It would work just as well.
    – frabjous
    Mar 19 at 16:24
  • @frabjous, if you symlink your scripts folder to the ~/bin it will appear there as a subfolder, but PATH will not know about this subfolder and you won't be able to run your script without its path. The purpose of the operation I propose is that the scripts from my ~/scripts folder appear inside the ~/bin folder and then I can run my scripts on terminal by a simple name without a path and without changing of the PATH variable. Mar 23 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.