32

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?

4 Answers 4

26

For example let me assume, you have an executable myscript. You need to run it from a terminal as,

/path/to/myscript

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), as

myscript

But 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,

/path/to/myscript

Also, you will not be able to run the script as sudo as it is not in PATH of root, To run as sudo you need to use,

sudo /path/to/myscript

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)

myscript

In that case you can run the executable as sudo also as,

sudo myscript

Now choose one way depending upon your need.

7

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.

7

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 the PATH, then any executable file present in that directory can be launched by only typing its name.

5
  • 1
    This information is false, you can stil launch the executable even if you are not in "that directory".
    – Lekensteyn
    Mar 29, 2014 at 14:27
  • How is this false?, please explain a bit more. Mar 29, 2014 at 14:42
  • 1
    Ok, I had a hard time reading your answer (and thereby made an error) so I edited it to remove the confusion and remoted the downvote. Hopefully it helps.
    – Lekensteyn
    Mar 29, 2014 at 16:21
  • So, the question is: What is better? Is there any difference? You practically didn't say nothing more than the OP. Mar 30, 2014 at 7:11
  • I answered for the OS's question but not in a brief format. Mar 30, 2014 at 7:46
1

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:

myscript

Just my 2 cents. :)

PS- I tried to post this as a comment to @souravc answer, but I didn't have enough reputation. :-(

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .