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?

nacho4d
  • 263

2 Answers2

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

  • (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 '22 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. – almaceleste Mar 19 '22 at 15:20
  • @frabjous, anyway, I'm not saying how you SHOULD - I'm just suggesting how you CAN store your scripts. – almaceleste Mar 19 '22 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 '22 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. – almaceleste Mar 23 '22 at 11:07
  • Sorry, but that's untrue. If you symlink the folder to the ~/bin it shows up as the ~/bin folder, unless you did the command wrong. – frabjous Mar 23 '22 at 12:29
  • @frabjous, if you create a symlink to the bin folder you need to delete the real bin folder else the symlink will be placed into it. But if you bind your folder to the bin it takes bin folder's place and the real bin folder stay unchanged. But I find out a flaw in my method I did not see earlier: the content of the real bin folder become inaccessible. – almaceleste Mar 23 '22 at 19:01
  • I doubt the OP has a ~/bin folder to delete. The first part of your answer, keeping things in ~/bin is good advice. I do that myself. I just think your answer would be improved if (1) you mentioned you might need to add ~/bin to your path anyway, since not all distros do that, and (2) there are alternatives to the binding strategy if you don't have root access. In my experience with your method, the contents of the ~/bin folder are still accessible, but if you modify anything in there, it's not reflected in the lowerdir. – frabjous Mar 23 '22 at 23:28
  • @frabjous, I don't use the ~/bin folder for my own scripts to avoid getting mixed up with files stored there by installed programs. So I created a ~/Documents/scripts folder and use it for my scripts. I mount it as a lower layer so third party programs can't change them, but they are visible in the ~/bin folder so I can run them by their simple name. I change them via ~/Documents/scripts path and everything is fine. And I mount the ~/bin folder as the upper layer so third party files are stored there as usual. – almaceleste Mar 25 '22 at 01:40
  • The answer is overly complex. It discusses things that were not asked for (overlay). – ctrl-alt-delor Jul 29 '23 at 10:37