82

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?

Seth
  • 58,122
Pandya
  • 35,771
  • 44
  • 128
  • 188

3 Answers3

103

It depends on who will use your script:

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

Twinkles
  • 1,157
  • 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 '14 at 12:12
  • 2
    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 '14 at 12:45
  • 1
    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 '14 at 14:19
  • 1
    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 '14 at 14:49
  • And, four years later, the recommendation still stands: FHS 3.0 – Twinkles Jul 13 '18 at 07:29
  • 1
    @Twinkles Is there any reason against putting user scripts in ~/.local/bin? (which is already in the PATH in a couple distributions) – Joschua May 31 '19 at 21:59
  • No, that's perfectly fine. In fact, should the executable be placed in the home directory automatically by a software, I'd recommend it. – Twinkles Jun 03 '19 at 08:48
  • Would you expect $HOME/.local/bin to be precreated on a ubuntu install? It wasn't for me nor is it in my $PATH. (Ubuntu 20.04.2 LTS on WSL2) – Iain Feb 08 '22 at 03:18
  • The XDG Desktop Specification asks distributions to add $HOME/.local/bin to $PATH. Ubuntu only does so if that directory exists. So, after you create that folder, simply source ~/.profile or log out and back in to have it in your PATH. – Twinkles Feb 10 '22 at 10:54
77

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.

Rinzwind
  • 299,756
  • 1
    At least in my installation of Ubuntu 14.04, executables in /usr/local/bin are not available to cron jobs (run by the web user). However, programs in /usr/bin are. – juacala Feb 03 '18 at 16:40
  • And how do I run my .sh file? Take a hello-world.sh file for example. – Enrique Bermúdez Mar 10 '20 at 12:42
  • 1
    @EnriqueBermúdez put #!/bin/sh at the first line, chmod it +x and put it in /usr/local/bin/. The extension is not needed. when you run it it will execute your script with /bin/sh. You can also put /bin/node there for a javascript file. – Antoni Jul 11 '21 at 07:30
58

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.

AzkerM
  • 10,270
  • 6
  • 32
  • 51
waltinator
  • 36,399
  • 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 '14 at 05:17
  • 8
    @JacobVlijm: It's "found by default" since it's added to $PATH in ~/.profile. ;) – Gunnar Hjalmarsson May 13 '14 at 06: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 '14 at 08:16
  • 3
    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 '14 at 03: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 '14 at 06:27
  • @Sparhawk - It's a personal preference. The reasons I advise against it are (only if you use custom programs with names of existing commands): 1) you get used to it and then it doesn't work when you are on another system and 2) if anyone else uses your system (e.g. to help you debug a problem), then they get unexpected behavior for commands with the same names. If you don't use the same names, then it's just faster to prepend the path as you do. – Joe May 16 '14 at 05:39
  • @Joe Yes, I agree that it's a personal preference, but I was just mentioning that others may have different preferences. FWIW I also have things like alias cp='cp -i --preserve=all' on my system, so I certainly don't use a "default" system. I copy my .bashrc to new servers, etc. – Sparhawk May 16 '14 at 05:59
  • @Joe fwiw, Ubuntu prepends. It has this in the default profile: PATH="$HOME/bin:$PATH" – Corey Goldberg Feb 24 '16 at 01:34
  • what about ~/.local/bin – Code42 Mar 19 '19 at 00:44
  • 1
    Just had the same question: ~/.local/bin is already in the PATH. Any reason against using it for personal scripts? – Joschua May 31 '19 at 12:02
  • I'm disinclined to put scripts in ~/.local/bin as tools like pip will also install to that directory. – xikkub Nov 07 '20 at 06:44