I have an executable. I want to execute the executable in terminal with name only like other commands.
I can put my executable in /usr/local/bin
or I could add its PATH
to ~/.bashrc
. Both will work.
What is better? Is there any difference?
I have an executable. I want to execute the executable in terminal with name only like other commands.
I can put my executable in /usr/local/bin
or I could add its PATH
to ~/.bashrc
. Both will work.
What is better? Is there any difference?
For example let me assume, you have an executable
myscript
. You need to run it from a terminal as,User level Change
If you add the
PATH
of that executable to~/.bashrc
, you can run the executable with name only from anywhere (Avinash Raj already mentioned), asBut the change will be affected in user level. That means if you have any other user(s) they could not access the executable with name only. If they have proper permission they need to run the executable as,
Also, you will not be able to run the script as sudo as it is not in
PATH
of root, To run assudo
you need to use,system level change
If you put your script in
/usr/local/bin
it can be accessed system wide and for all users. In that case any user can run your executable as (subject to having proper permissions)In that case you can run the executable as
sudo
also as,Now choose one way depending upon your need.
Adding the location of the file to your
$PATH
variable in your~/.bashrc
file will only allow you to execute from any location, whereas putting it in/usr/bin/
will allow all users on your system to execute that file from any location.Why is that? Because, your
~/.bashrc
file is only visible to you as a user. So all variable changes done are limited to you. Whereas, adding that file to/usr/bin
will allow the file to remain there for all users and since/usr/bin
is present in the$PATH
variable unless someone removes it, will allow all users to execute it from any location.If you put the executable file in
/usr/bin
or/usr/local/bin
, then you will be able to run that program by only specifying the name (your-program
instead of/usr/local/bin/your-program
).If you instead add the directory containing the program to the
~/.bashrc
file to thePATH
, then any executable file present in that directory can be launched by only typing its name.Just as a complement to @souravc answer...
For the "User level Change", instead of editing the
~/.bashrc
file you can simply create the directory$HOME/bin/
and place your scripts in there.The directory will be automatically added to the
PATH
(at least since Ubuntu 12.04) which means that you'll be able to run all the scripts/executables inside that directory with a simple:Just my 2 cents. :)
PS- I tried to post this as a comment to @souravc answer, but I didn't have enough reputation. :-(