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?
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:
/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
.)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
).
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 tofstab
:
# bind my scripts to ~/bin
/home/user/my-scripts-directory /home/user/bin none bind 0 0
~/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.
~/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, 2022 at 11:07