Keeping a Private Stash of Utilities by Adding ~/bin

Problem

You have a stash of personal utilities you like to use, but you are not root on the system and can’t place them into the normal locations like /bin or /usr/local/bin, or there is some other reason to separate them.

Solution

Create a ~/bin directory, place your utilities in it and add it to your path:

$ PATH="$PATH:~/bin"

You’ll want to make this change in one of your shell initialization files, such as ~/.bashrc. Some systems already add $HOME/bin as the last directory in a nonprivileged user account by default, so check first.

Discussion

As a fully qualified shell user (well, you bought this book), you’ll certainly be creating lots of scripts. It’s inconvenient to invoke scripts with their full pathname. By collecting your scripts in a ~/bin directory, you can make your scripts look like regular Unix programs—at least to you.

For security reasons, don’t put your bin directory at the start of your path. Starting your path with ~/bin makes it easy to override system commands—which is inconvenient, if it happens accidentally (we’ve all done it), and dangerous if it’s done maliciously.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.