Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Where can (should) I put my (bash) script so that it can be used (forever) by terminal or by a direct command: Alt+F2?

I know there is /usr/bin and /sbin & /bindirectories but when should I use between them?

Where should I put my script?

share|improve this question
7  
Please read this, and this questions to understand all about this directories –  c0rp May 13 at 4:57

3 Answers 3

up vote 18 down vote accepted

Don't use these directories:

/usr/bin, /sbin and /bin

Leave them for package-managed executables.


If you need the script for one user, waltinator's answer is fine.

If you need the script for all users on your system (but you can also use this for one user), stick it in /usr/local/bin/. One advantage: this directory is already in your PATH so there is no need to edit files.

share|improve this answer
1  
Seriously? 18 upvotes, accepted and I get a downvote without a comment why?! How am I suppose to improve my answer if I can not see a problem? –  Rinzwind May 21 at 7:48

It depends on who will use your script:

  • Yourself only - $HOME/bin like @waltinator said
  • You and other local users - /usr/local/bin
  • root only - /usr/local/sbin

That way you have your own scripts separated from the distribution-provided binaries.

share|improve this answer
    
Traditionally, the .../sbin directories are used for statically-linked binaries (mostly used by root, before shared libraries are available), not user scripts. –  waltinator May 13 at 12:12
1  
That only applies to /sbin, not /usr/sbin or /usr/local/sbin. The Filesystem Hierarchy Standard states that "Locally installed system administration programs should be placed in /usr/local/sbin." [link]. –  Twinkles May 13 at 12:45
    
This is all true, but the sbin directories are being phased out and there really is no reason to separate them. For more on that, see here. –  terdon May 13 at 14:19
    
I see a discussion about phasing them out, and while the arguments are compelling, the current recommendation is the one I quoted above. –  Twinkles May 13 at 14:49

You should put your script under $HOME/bin. Follow below PATH to achieve this:

  1. Create a folder using mkdir $HOME/bin
  2. Then put your script in $HOME/bin

  3. Finally, add the following line under $HOME/.bashrc by editing with gedit $HOME/.bashrc

export PATH="$HOME/bin:$PATH"

When the system is looking for the command you typed, it will look in each directory of $PATH and execute the first match it finds.

share|improve this answer
3  
I am not sure I understand. First put it in $HOME/bin, then create the directory? Also, scripts in $HOME/bin are found by default, no need to add it to $PATH. –  Jacob Vlijm May 13 at 5:17
5  
@JacobVlijm: It's "found by default" since it's added to $PATH in ~/.profile. ;) –  Gunnar Hjalmarsson May 13 at 6:24
    
@GunnarHjalmarsson You are right, what I meant was: so you do'nt need to add it add it once more :) –  Jacob Vlijm May 13 at 8:16
2  
Adding $HOME/bin to the start of your path will make sure your scripts get seen before any elsewhere on the system. While this is convenient, it opens you up to all sorts of unexpected behaviors if one or more of your scripts has the same name as a command somewhere else in your path. This could potentially open security vulnerabilities as it it is much easier to gain access to a user account than one with elevated privileges. E.g. someone adding a script named ls to your bin directory that really runs an rm -rf * . Adding your $HOME/bin to the end of your path avoids most such problems. –  Joe May 15 at 3:35
    
@Joe If they have write access to $HOME/bin, then they can already rm -rf ~. I always prefer prepending custom paths, since it's an explicit decision I've made. –  Sparhawk May 15 at 6:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.