Change Your $PATH Permanently

Problem

You need to permanently change your path.

Solution

First you need to discover where the path is set, and then update it. For your local account, it’s probably set in ~/.profile or ~/.bash_profile. Find the file with grep -l PATH ~/.[^.]* and edit it with your favorite editor; then source the file to have the change take effect immediately.

If you are root and you need to set the path for the entire system, the basic procedure is the same, but there are different files in /etc where the $PATH may be set, depending on your operating system and version. The most likely file is /etc/profile, but /etc/bashrc, /etc/rc, /etc/default/login, ~/.ssh/environment, and the PAM /etc/ environment files are also possible.

Discussion

The grep -l PATH~/.[^.]* command is interesting because of the nature of shell wild-card expansion and the existence of the /. and /.. directories. See Showing All Hidden (dot) Files in the Current Directory, for details.

The locations listed in the $PATH have security implications, especially when you are root. If a world-writable directory is in root’s path before the typical directories (i.e., /bin, /sbin), then a local user can create files that root might execute, doing arbitrary things to the system. This is the reason that the current directory (.) should not be in root’s path either.

To be aware of this issue and avoid it:

  • Make root’s path as short as possible, and never use relative paths.

  • Avoid having world-writable directories ...

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.