How to permanently set $PATH on Linux/Unix?

I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:

export PATH=$PATH:/path/to/dir

This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.

How can I do it so this will be set permanently?


You need to add it to your ~/.profile or ~/.bashrc file.

export PATH=$PATH:/path/to/dir

Depending on what you're doing, you also may want to symlink to binaries:

cd /usr/bin
sudo ln -s /path/to/binary binary-name

Note that this will not automatically update your path for the remainder of the session. To do this, you should run:

source ~/.profile 
or
source ~/.bashrc

I can't believe nobody mentioned /etc/environment file. It's sole purpose is to store Environment Variables. Originally the $PATH variable is defined here. This is a paste from my /etc/environment file:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

So you can just open up this file as root and add whatever you want.

For Immediate results, Run (try as normal user and root):

source /etc/environment && export PATH

UPDATE:

If you use zsh (aka Z Shell), add this line right after the comments in /etc/zsh/zshenv :

source /etc/environment

I encountered this little quirk on Ubuntu 15.10, but if your zsh is not getting the correct PATH , this could be why


There are multiple ways to do it. The actual solution depends on the purpose.

The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax.

System wide

  • /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME .
  • /etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin . The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
  • /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  • /etc/<shell>.<shell>rc . Shell script. This is a poor choice because it is single shell specific.
  • User session

  • ~/.pam_environment . List of unique assignments. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variable including HOME or PATH so it has limited use.
  • ~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME . The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
  • ~/.profile Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems.
  • ~/.<shell>rc . Shell script. This is a poor choice because it is single shell specific.
  • Distribution specific documentation

  • Ubuntu
  • archlinux
  • 链接地址: http://www.djcxy.com/p/19180.html

    上一篇: 我需要改变一个sqlite3表来添加FTS3

    下一篇: 如何在Linux / Unix上永久设置$ PATH?