Adding a Directory to the Bash Path

How to add a directory to the Bash $PATH

This is a short and simple tip, but something I thought was worth remembering. I like to store my scripts in my ~/bin directory, keeping them all neat and organized. Rather than linking them to my /usr/local/bin directory or typing ~/bin/command-name, I prefer to simply add ~/bin to my Bash path, allowing me to type command-name. Some Linux distros do this for you, but some don't, so here is how to do it.

Simply create/edit the ~/.profile file and add the lines:

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

You can add other directories here, too. It simply checks that the directory exists and, if it does, adds it to the $PATH variable. After relogging, you will see that you can now run commands in that directory as if it was linked to the /usr/local/bin directory. Bash will automatically read this file every time you log on, so there are no other changes to make!

You must log in to post a comment.