How to permanently set $PATH on Linux/Unix?

2018-12-31 04:26发布

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?

21条回答
墨雨无痕
2楼-- · 2018-12-31 04:58

the best simple way is the following line:
PATH="<directory you want to include>:$PATH"
in your .bashrc file in home directory.
It will not get reset even if you close the terminal or reboot your PC. Its permanent

查看更多
余欢
3楼-- · 2018-12-31 04:59

I stumbled across this question yesterday when searching for a way to add a folder containing my own scripts to the PATH - and was surprised to find out that my own ~/.profile file (on Linux Mint 18.1) already contained this:

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

Thus, all I had to do was create the folder ~/bin and put my scripts there.

查看更多
闭嘴吧你
4楼-- · 2018-12-31 05:01

In Ubuntu, edit /etc/environment. Its 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 (a.k.a 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

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 05:01

the files where you add the export command depends if you are in login-mode or non-login-mode.

if you are in login-mode, the files you are looking for is either /etc/bash or /etc/bash.bashrc

if you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d

the files mentioned above if where the system variables are.

查看更多
残风、尘缘若梦
6楼-- · 2018-12-31 05:02

Permanently add PATH variable

Global:

echo "export PATH=$PATH:/new/path/variable" >> /etc/profile

Local(for user only):

echo "export PATH=$PATH:/new/path/variable" >> ~/.profile

For global restart. For local relogin.

Example

Before:

$ cat /etc/profile 

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin

After:

$ cat /etc/profile 

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

Alternatively you can just edit profile:

$ cat /etc/profile 

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

Another way(thanks gniourf_gniourf):

echo 'PATH=$PATH:/new/path/variable' >> /etc/profile

You shouldn't use double quotes here! echo 'export PATH=$PATH:/new/path/variable'... And by the way, the export keyword is very likely useless as the PATH variable is very likely already marked as exported. – gniourf_gniourf

查看更多
余欢
7楼-- · 2018-12-31 05:05

For debian distribution, you have to:

    - edit ~/.bashrc  e.g: vim ~/.bashrc 
    - add export PATH=$PATH:/path/to/dir
    - then restart your computer. Be aware that if you edit ~/.bashrc  as root, your environment variable you added will work only for root
查看更多
登录 后发表回答