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?
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
inPATH
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~/bin
it will appear there as a subfolder, butPATH
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 thePATH
variable. – almaceleste Mar 23 '22 at 11:07~/bin
it shows up as the~/bin
folder, unless you did the command wrong. – frabjous Mar 23 '22 at 12:29~/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~/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 alower
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 theupper
layer so third party files are stored there as usual. – almaceleste Mar 25 '22 at 01:40