Where should I put my Bash scripts?

14,492

Solution 1

I save my own scripts in /opt/scripts.

If your script should executeable by every system user, you can create a symbolic link to /usr/bin.

If only root should execute the script, you can create a symbolic link to /usr/sbin.

Command to add a symbolic link in /usr/bin/:

ln -s /opt/scripts/<script> /usr/bin/

You can execute the script, because /usr/bin/ is in your PATH by default.

Solution 2

If no other user other than you uses these scripts

Then you can keep them in /home/$USER/bin. Create the bin directory if it doesn't exist and move the files there. The bin directory in your home will automatically get added to the PATH environment variable. The code is in the .profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

See How to add /home/username/bin to $PATH?

Or in some systems it may be in .bashrc:

export PATH=${HOME}/bin/:${HOME}/.local/bin:${PATH}

Thanks Elder Geek.

If these script are to be used by other users:

Then either /usr/local/bin or /opt/bin are good options. See Is there a standard place for placing custom Linux scripts?

Solution 3

I have a directory that I use for the quick collection of my local tools or things that I deploy on various computers in /usr/local/apollo. There are branches off this directory for flags, bin and logs.

For the applications that I download and install outside of the default apt-get repositories are placed in /opt/ and a directory by the app's name, with one more sub-directory for the specific version of the application. This way my compiled version of an application like vlc or eclipse won't conflict with the distributed version.

My use of /opt is the way it's basically officially designed.

By the way the directories /usr/local/bin, /usr/local/apollo, and /opt survives a fresh OS version installation overwrite.

Share:
14,492

Related videos on Youtube

hatterman
Author by

hatterman

I was first exposed to Linux around 15 years ago, the equipment I support ran on Red Hat. Nowadays it's all open SuSE. The more I learned the more I liked it and I eventually moved over from the dark side and installed Ubuntu on my home PCs about 2 years ago. I do still have Windows, I am not one to get involved in the operating system arguments, but i find I am using it less and less. I run my main PC and laptop on Ubuntu 14.04 LTS with the Gnome desktop. I run my Intel Atom based netbook on Ubuntu 14.04 LTS with the Lxde desktop. My preference these days is to install from the minimal ISO and add the desktop environment and chosen applications on top. I run my Raspberry Pi on Raspbian and use it as an 'always on' remote access port for when I am away from home. When I am connected to a random hotel wifi I ssh into my Pi and, with dynamic port forwarding, route all my traffic through it. The Pi uses around 2W, it's my best gadget of the year. I run my server on Open Media Vault using an old cube PC I salvaged from the work skip. This shuts down automatically and I wake it, as needed, with a WOL command from the Pi. I am by no means an expert but an enthusiastic amatuer. I try to provide help on here and often find my self googling around in order to answer a question. I find I am learning more myself when I try to answer questions.

Updated on September 18, 2022

Comments

  • hatterman
    hatterman almost 2 years

    I have a few very simple Bash scripts that I cobbled together for things that I do regularly.

    One of them is to run duplicity to do my backup tasks. Nothing clever, just a bunch of if .. then statements really.

    As this needs to be run as root, would it be best practice to put my script in /usr/bin (or another location on PATH), chown to root:root and chmod to 700?

    • Admin
      Admin over 7 years
      I would say use git to version-control your scripts, put local copies of the git repos somewhere you like in ~, and then symlink the scripts into ~/bin.
    • Admin
      Admin over 7 years
      You mean git as in github in the clouds?
    • Admin
      Admin over 7 years
      I think he means for you to create local repositories with git (on your machine only) not remote ones like those on github. This latter would only be useful if you wanted to share your scripts with others.
    • Admin
      Admin over 7 years
      @edwinksl the think I don't like about putting them in /home/me/bin is remembering to get new user accounts permission to the directory and putting it in their path. Is there a disadvantage to /usr/local like it goes poof when an upgrade is done?
    • Admin
      Admin over 7 years
      @WinEunuuchs2Unix If you want your scripts to be available to other users, you should put them in /usr/local/bin. Otherwise, I would say just put them in ~/bin. Your own scripts in both directories should be safe when you upgrade.
    • Admin
      Admin over 7 years
      As above, place them in /usr/local/bin. Just make sure your script names are unique & not an existing linux command/binary name. Myself just add a number to end of any script I create as I haven't seen any pre-existing linux names end in a number. ( not to say some really obscure ones may...
    • Admin
      Admin over 7 years
      @doug the number suffix would also come in handy when you are testing different flavors of your scripts.
    • Admin
      Admin over 7 years
      @edwinksl my apologies L.D.James just explained to me how ~/bin is dynamically placed in PATH$ during login.
    • Admin
      Admin almost 7 years
      @edwinksl Almost a year later I have to say ~/bin is the best place for most scripts as you don't have to use sudo to edit them as you do when they are stored in /usr/local/bin.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin‌​:/usr/games:/usr/loc‌​al/games:/snap/bin I like the fact /usr/local/bin is already in the path one less thing to remember. I like your method of /opt/program/version I can use that for Kernel stuff I get and compile like EnhanceIO where they change things between kernel versions. Is /apollo a moon landing personal favorite or does it have Ubuntu meaning?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    What is the difference between /usr/local/bin and usr/local/sbin such that the latter gets nuked during upgrades?
  • Apologician
    Apologician over 7 years
    The installer Nukes the directories that it uses. It conveniently creates /usr/local directories, but it doesn't put anything in any of them. Those directories are populated by the user. Many source programs outside the repositories give the user the option of selecting where they want the install to go. The default in the config files are /usr/local/bin . So because of how common it is to use those directories, they are included in the user's path by default. By default the system checks for ~/bin and adds it to the path if it exists.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    You mean the system checks for ~/bin during install or every boot? What is the difference between sbin and bin? They seem to coexist there must be quasi-rules on which you pick based on program type right?
  • Apologician
    Apologician over 7 years
    Apollo was the name of a band I had when I was young. It because the name of my company later. Apollo is named after the Greek God Apollo. The /usr/local/sbin is for system applications that are compiled on the local. The /usr/local/bin is for general applications compiled on the local machine.
  • Apologician
    Apologician over 7 years
    @WinEunuuchs2Unix On the ~/bin path... it's not added during installation. The system checks for it on every login and adds it to the $PATH if it exist during the login. Look at the last two lines of your ~/.profile settings.
  • Apologician
    Apologician over 7 years
  • shalomb
    shalomb over 6 years
    I would recommend that instead of using /usr/bin as the target for user/local shell script - that it be /usr/local/bin (or /opt/bin) as per Filesystem Hierarchy Standard - Debian Wiki to avoid conflicts (most of the time, you want Ubuntu's provided scripts to take precedence).
  • allo
    allo over 6 years
    On most systems /usr/local/bin overrides /usr/bin, as it comes later in the path. This is on purpose, as the system does not put files there, so you can put files there, which SHOULD override the system provided ones.
  • hatterman
    hatterman over 6 years
    I marked this as the correct answer, even though both answers seem fine. Reason is, I eneded up taking a look at the FHS docs and came away with the understanding that /opt is there for exactly this purpose. I like the idea of them simply sym linking to my scripts in /usr/local/bin. Thanks for all the pointers.
  • Admin
    Admin about 2 years
    For user-specific scripts and binaries there's ~/.local/bin. See this and this for explanations.