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:46

Put the export declaration in ~/.bashrc. My .bashrc contains this:

export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH
查看更多
不再属于我。
3楼-- · 2018-12-31 04:48

My answer is in reference to the setting-up of go-lang on Ubuntu linux/amd64.I have faced the same trouble of setting the path of environment variables (GOPATH and GOBIN), losing it on terminal exit and rebuilding it using the source <file_name> every time.The mistake was to put the path (GOPATH and GOBIN) in ~/.bash_profile folder. After wasting a few good hours, I found that the solution was to put GOPATH and GOBIN in ~/.bash_rc file in the manner:

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN

and doing so, the go installation worked fine and there were no path losses.

EDIT 1: The reason with which this issue can be related is that settings for non-login shells like your ubuntu terminal or gnome-terminal where we run the go code are taken from ~./bash_rc file and the settings for login shells are taken from ~/.bash_profile file, and from ~/.profile file if ~/.bash_profile file is unreachable.

查看更多
无色无味的生活
4楼-- · 2018-12-31 04:49

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
查看更多
余生请多指教
5楼-- · 2018-12-31 04:50

I think the most elegant way is:

1.add this in ~/.bashrc file Run this command

gedit ~/.bashrc

add your path inside it

export PATH=$PATH:/opt/node/bin

2.source ~/.bashrc

(Ubuntu)

查看更多
还给你的自由
6楼-- · 2018-12-31 04:50

You can add that line to your console config file (e.g. .bashrc) , or to .profile

查看更多
美炸的是我
7楼-- · 2018-12-31 04:51

1.modify "/etc/profile" file.

#vi /etc/profile

Press "i" key to enter editing status and move cursor to the end of the file,Additional entries:

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

Press "Esc" key exit edit status,':wq' save the file.

2.Make configuration effective

source /etc/profile

Explain: profile file works for all users,if you want to be valid only for the active user, set the ".bashrc" file

查看更多
登录 后发表回答