如何在Linux/Unix上永久设置$PATH ?

时间:2021-01-02 23:07:21

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

我试图在路径中添加一个目录,这样它就会一直在我的Linux路径中。我试过了:

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.

然而,每当我退出终端并启动一个新的终端实例时,这条路径就会丢失,我需要再次运行export命令。

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

我该怎么做呢?

20 个解决方案

#1


699  

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

你需要把它添加到你的~/中。配置文件或~ /。bashrc文件。(

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

#2


192  

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:

我不敢相信没有人提到/etc/environment文件。它的唯一目的是存储环境变量。$PATH变量最初是在这里定义的。这是我的/etc/environment文件中的一个粘贴:

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:

如果你使用zsh (a.k。a Z Shell),在/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

我在Ubuntu 15.10上遇到了这个小问题,但是如果你的zsh没有得到正确的路径,这可能就是原因

#3


103  

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.

变量值通常存储在分配列表或在系统或用户会话开始时运行的shell脚本中。在shell脚本中,必须使用特定的shell语法。

System wide

  1. /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME.
  2. /etc/environment唯一分配的列表。完美地添加系统范围的目录,如/usr/local/something/bin到PATH变量或定义JAVA_HOME。
  3. /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.
  4. 在启动X窗口系统会话时执行/etc/xprofile Shell脚本。这是为每个登录到X窗口系统的用户运行的。对于每个用户(如/usr/ local/something/bin)都有效的路径条目,这是一个很好的选择。其他脚本包含该文件,因此使用POSIX shell语法而不是用户shell语法。
  5. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  6. / etc / profile和/etc/profile.d / * Shell脚本。这对于纯shell系统是一个很好的选择。那些文件只能由shell读取。
  7. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
  8. /etc/ <壳> 。 <壳> rc。Shell脚本。这是一个糟糕的选择,因为它是单壳的。

User session

  1. ~/.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.
  2. ~ / .pam_environment。独特的任务列表。如果是X窗口系统会话或shell,则由PAM在每个用户会话开始时加载。您不能引用包括HOME或PATH在内的其他变量,因此它的用途有限。
  3. ~/.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.
  4. ~ /。xprofile Shell脚本。当用户登录到X窗口系统时执行此操作。这里定义的变量对每个X应用程序都是可见的。使用~/bin或~/go/bin或定义特定用户GOPATH或NPM_HOME等值扩展路径的完美选择。其他脚本包含该文件,因此使用POSIX shell语法而不是用户shell语法。您的图形文本编辑器或IDE以快捷方式开始,将看到这些值。
  5. ~/.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.
  6. ~ /。Shell脚本。它只对从终端或终端模拟器启动的程序可见。对于纯shell系统来说,这是一个很好的选择。
  7. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
  8. ~ / <壳> rc。Shell脚本。这是一个糟糕的选择,因为它是单壳的。

Distribution specific documentation

#4


65  

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

把出口申报单放在~/.bashrc中。我的. bashrc包含:

export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH

#5


28  

You may set $PATH permanently in 2 ways.

您可以用两种方式永久性地设置$PATH。

  1. To set path for particular user : You may need to make the entry in .bash_profile in home directory in the user.

    要为特定用户设置路径:您可能需要在用户的主目录中的.bash_profile中设置条目。

    e.g in my case I will set java path in tomcat user profile

    e。在我的例子中,我将在tomcat用户配置文件中设置java路径

    [tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
    
  2. To set common path for ALL system users, you may need to set path like this :

    要为所有系统用户设置公共路径,您可能需要如下设置路径:

    [root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
    

#6


9  

You can use on Centos or RHEL for local user:

本地用户可使用Centos或RHEL:

echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile

This add the current directory(or you can use other directory) to the PATH, this make it permanent but take effect at the next user logon.

这将把当前目录(或者您可以使用其他目录)添加到路径中,这使它成为永久性的,但在下一次用户登录时生效。

If you don't want do a re-logon, then can use:

如果你不想重新登录,可以使用:

source ~/.bash_profile

That reload the # User specific environment and startup programs this comment is present in .bash_profile

重新加载# User特定的环境和启动程序。bash_profile中显示的注释

#7


5  

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

您可以将这一行添加到控制台配置文件(例如.bashrc)或.profile中

#8


5  

I think the most elegant way is:

我认为最优雅的方式是:

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

1。在~ /添加这个。bashrc文件运行此命令

gedit ~/.bashrc

中~ / . bashrc

add your path inside it

在其中添加路径

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

导出路径= $路径:/ bin / opt /节点

2.source ~/.bashrc

2。源~ / . bashrc

(Ubuntu)

(Ubuntu)

#9


4  

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:

昨天我在寻找一种方法将包含我自己脚本的文件夹添加到路径中时,偶然发现了这个问题,我惊讶地发现我自己的~/。概要文件(在Linux Mint 18.1上)已经包含以下内容:

# 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.

因此,我所要做的就是创建文件夹~/bin并将脚本放在那里。

#10


3  

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

如果您处于登录模式,您要查找的文件要么是/etc/bash,要么是/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

如果您处于非登录模式,您正在查找文件/。配置文件或目录/.profiles.d中的文件

the files mentioned above if where the system variables are.

如果系统变量在上面提到的文件。

#11


3  

Zues77 has the right idea. The OP didn't say "how can i hack my way through this". OP wanted to know how to permanently append to $PATH:

Zues77的想法是对的。操作人员并没有说“我怎么才能通过这条路”。OP想知道如何永久地追加到$PATH:

sudo nano /etc/profile

This is where it is set for everything and is the best place to change it for all things needing $PATH

这是为所有需要$PATH的东西设置的位置,也是更改它的最佳位置

#12


3  

You can also set permanently, editing one of these files:

你也可以设置永久,编辑其中一个文件:

/etc/profile (for all users)

/ etc / profile(所有用户)

~/.bash_profile (for actual user)

~ /。bash_profile(实际用户)

~/.bash_login (for actual user)

~ /。bash_login(实际用户)

~/.profile (for actual user)

~ /。实际配置文件(用户)

You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.

您还可以使用/etc/environment设置一个永久路径环境变量,但它不支持变量展开。

Extracted from: http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

提取:http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

#13


3  

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:

我的答案是关于在Ubuntu linux/amd64上建立go-lang。我也遇到过设置环境变量(GOPATH和GOBIN)路径的麻烦,在终端退出时丢失它,每次使用源 重新构建它。错误在于把路径(GOPATH和GOBIN)放在~/中。bash_profile文件夹。浪费了几个小时之后,我发现解决的办法是把GOPATH和GOBIN放到~/中。bash_rc文件的方式:

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.

这样做,go安装工作良好,没有路径损失。

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.

编辑1:这个问题的原因是,像你的ubuntu终端或gnome-terminal这样的非登录shell的设置是从~开始的。/bash_rc文件和登录shell的设置来自~/。bash_profile文件,以及from ~/。如果~ /概要文件。bash_profile文件是遥不可及的。

#14


2  

Add to /etc/profile.d folder script [name_of_script].sh with line: export PATH=$PATH:/dir. Every script within /etc/profile.d folder is automaticaly executed by /etc/profile on login.

添加到/etc/profile.d脚本文件夹(name_of_script)。带线的sh: export PATH=$PATH:/dir。每一个脚本在/etc/profile.d文件夹是由/etc/profile在登录时自动执行的。

#15


1  

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

最好的简单方法是:PATH=" <目录,您希望在主目录的.bashrc文件中包含> :$PATH"。即使你关闭终端或重启电脑,它也不会被重置。它的永久

#16


1  

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.

为全球重新启动。为当地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):

另一种方式(由于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

你不应该在这里使用双引号!回声的出口路径= $路径:/新/ PATH /变量”……顺便说一下,export关键字很可能是无用的,因为PATH变量很可能已经标记为export。——gniourf_gniourf

#17


1  

I think the most elegant way is:

我认为最优雅的方式是:

1.add this in ~./bashrc file

1。添加这个~。/ bashrc文件(

if [ -d "new-path" ]; then
  PATH=$PATH:new-path
fi

2.source ~/.bashrc

2。源~ / . bashrc

(Ubuntu)

(Ubuntu)

#18


0  

one way to add permanent path, which worked for me, is: cd /etc/profile.d touch custom.sh vi custom.sh export PATH=$PATH:/path according to your setting/ restart your computer and here we go path will there permanently cheers.

添加永久路径的一种方法对我来说是:cd /etc/ profile.com。d碰定制。sh vi的风俗。sh export PATH=$PATH:/ PATH根据您的设置/重新启动您的计算机,这里我们选择PATH将永远为您欢呼。

#19


0  

For debian distribution, you have to:

对于debian发行版,您必须:

    - 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

#20


-5  

It can be directly added by using the following command:

可以使用以下命令直接添加:

echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc source ~/.zshrc

echo 'export PATH=$PATH:/new/directory' >> ~/。~ / . zshrc zshrc来源

#1


699  

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

你需要把它添加到你的~/中。配置文件或~ /。bashrc文件。(

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

#2


192  

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:

我不敢相信没有人提到/etc/environment文件。它的唯一目的是存储环境变量。$PATH变量最初是在这里定义的。这是我的/etc/environment文件中的一个粘贴:

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:

如果你使用zsh (a.k。a Z Shell),在/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

我在Ubuntu 15.10上遇到了这个小问题,但是如果你的zsh没有得到正确的路径,这可能就是原因

#3


103  

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.

变量值通常存储在分配列表或在系统或用户会话开始时运行的shell脚本中。在shell脚本中,必须使用特定的shell语法。

System wide

  1. /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME.
  2. /etc/environment唯一分配的列表。完美地添加系统范围的目录,如/usr/local/something/bin到PATH变量或定义JAVA_HOME。
  3. /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.
  4. 在启动X窗口系统会话时执行/etc/xprofile Shell脚本。这是为每个登录到X窗口系统的用户运行的。对于每个用户(如/usr/ local/something/bin)都有效的路径条目,这是一个很好的选择。其他脚本包含该文件,因此使用POSIX shell语法而不是用户shell语法。
  5. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  6. / etc / profile和/etc/profile.d / * Shell脚本。这对于纯shell系统是一个很好的选择。那些文件只能由shell读取。
  7. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
  8. /etc/ <壳> 。 <壳> rc。Shell脚本。这是一个糟糕的选择,因为它是单壳的。

User session

  1. ~/.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.
  2. ~ / .pam_environment。独特的任务列表。如果是X窗口系统会话或shell,则由PAM在每个用户会话开始时加载。您不能引用包括HOME或PATH在内的其他变量,因此它的用途有限。
  3. ~/.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.
  4. ~ /。xprofile Shell脚本。当用户登录到X窗口系统时执行此操作。这里定义的变量对每个X应用程序都是可见的。使用~/bin或~/go/bin或定义特定用户GOPATH或NPM_HOME等值扩展路径的完美选择。其他脚本包含该文件,因此使用POSIX shell语法而不是用户shell语法。您的图形文本编辑器或IDE以快捷方式开始,将看到这些值。
  5. ~/.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.
  6. ~ /。Shell脚本。它只对从终端或终端模拟器启动的程序可见。对于纯shell系统来说,这是一个很好的选择。
  7. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
  8. ~ / <壳> rc。Shell脚本。这是一个糟糕的选择,因为它是单壳的。

Distribution specific documentation

#4


65  

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

把出口申报单放在~/.bashrc中。我的. bashrc包含:

export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH

#5


28  

You may set $PATH permanently in 2 ways.

您可以用两种方式永久性地设置$PATH。

  1. To set path for particular user : You may need to make the entry in .bash_profile in home directory in the user.

    要为特定用户设置路径:您可能需要在用户的主目录中的.bash_profile中设置条目。

    e.g in my case I will set java path in tomcat user profile

    e。在我的例子中,我将在tomcat用户配置文件中设置java路径

    [tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
    
  2. To set common path for ALL system users, you may need to set path like this :

    要为所有系统用户设置公共路径,您可能需要如下设置路径:

    [root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
    

#6


9  

You can use on Centos or RHEL for local user:

本地用户可使用Centos或RHEL:

echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile

This add the current directory(or you can use other directory) to the PATH, this make it permanent but take effect at the next user logon.

这将把当前目录(或者您可以使用其他目录)添加到路径中,这使它成为永久性的,但在下一次用户登录时生效。

If you don't want do a re-logon, then can use:

如果你不想重新登录,可以使用:

source ~/.bash_profile

That reload the # User specific environment and startup programs this comment is present in .bash_profile

重新加载# User特定的环境和启动程序。bash_profile中显示的注释

#7


5  

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

您可以将这一行添加到控制台配置文件(例如.bashrc)或.profile中

#8


5  

I think the most elegant way is:

我认为最优雅的方式是:

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

1。在~ /添加这个。bashrc文件运行此命令

gedit ~/.bashrc

中~ / . bashrc

add your path inside it

在其中添加路径

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

导出路径= $路径:/ bin / opt /节点

2.source ~/.bashrc

2。源~ / . bashrc

(Ubuntu)

(Ubuntu)

#9


4  

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:

昨天我在寻找一种方法将包含我自己脚本的文件夹添加到路径中时,偶然发现了这个问题,我惊讶地发现我自己的~/。概要文件(在Linux Mint 18.1上)已经包含以下内容:

# 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.

因此,我所要做的就是创建文件夹~/bin并将脚本放在那里。

#10


3  

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

如果您处于登录模式,您要查找的文件要么是/etc/bash,要么是/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

如果您处于非登录模式,您正在查找文件/。配置文件或目录/.profiles.d中的文件

the files mentioned above if where the system variables are.

如果系统变量在上面提到的文件。

#11


3  

Zues77 has the right idea. The OP didn't say "how can i hack my way through this". OP wanted to know how to permanently append to $PATH:

Zues77的想法是对的。操作人员并没有说“我怎么才能通过这条路”。OP想知道如何永久地追加到$PATH:

sudo nano /etc/profile

This is where it is set for everything and is the best place to change it for all things needing $PATH

这是为所有需要$PATH的东西设置的位置,也是更改它的最佳位置

#12


3  

You can also set permanently, editing one of these files:

你也可以设置永久,编辑其中一个文件:

/etc/profile (for all users)

/ etc / profile(所有用户)

~/.bash_profile (for actual user)

~ /。bash_profile(实际用户)

~/.bash_login (for actual user)

~ /。bash_login(实际用户)

~/.profile (for actual user)

~ /。实际配置文件(用户)

You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.

您还可以使用/etc/environment设置一个永久路径环境变量,但它不支持变量展开。

Extracted from: http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

提取:http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

#13


3  

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:

我的答案是关于在Ubuntu linux/amd64上建立go-lang。我也遇到过设置环境变量(GOPATH和GOBIN)路径的麻烦,在终端退出时丢失它,每次使用源 重新构建它。错误在于把路径(GOPATH和GOBIN)放在~/中。bash_profile文件夹。浪费了几个小时之后,我发现解决的办法是把GOPATH和GOBIN放到~/中。bash_rc文件的方式:

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.

这样做,go安装工作良好,没有路径损失。

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.

编辑1:这个问题的原因是,像你的ubuntu终端或gnome-terminal这样的非登录shell的设置是从~开始的。/bash_rc文件和登录shell的设置来自~/。bash_profile文件,以及from ~/。如果~ /概要文件。bash_profile文件是遥不可及的。

#14


2  

Add to /etc/profile.d folder script [name_of_script].sh with line: export PATH=$PATH:/dir. Every script within /etc/profile.d folder is automaticaly executed by /etc/profile on login.

添加到/etc/profile.d脚本文件夹(name_of_script)。带线的sh: export PATH=$PATH:/dir。每一个脚本在/etc/profile.d文件夹是由/etc/profile在登录时自动执行的。

#15


1  

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

最好的简单方法是:PATH=" <目录,您希望在主目录的.bashrc文件中包含> :$PATH"。即使你关闭终端或重启电脑,它也不会被重置。它的永久

#16


1  

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.

为全球重新启动。为当地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):

另一种方式(由于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

你不应该在这里使用双引号!回声的出口路径= $路径:/新/ PATH /变量”……顺便说一下,export关键字很可能是无用的,因为PATH变量很可能已经标记为export。——gniourf_gniourf

#17


1  

I think the most elegant way is:

我认为最优雅的方式是:

1.add this in ~./bashrc file

1。添加这个~。/ bashrc文件(

if [ -d "new-path" ]; then
  PATH=$PATH:new-path
fi

2.source ~/.bashrc

2。源~ / . bashrc

(Ubuntu)

(Ubuntu)

#18


0  

one way to add permanent path, which worked for me, is: cd /etc/profile.d touch custom.sh vi custom.sh export PATH=$PATH:/path according to your setting/ restart your computer and here we go path will there permanently cheers.

添加永久路径的一种方法对我来说是:cd /etc/ profile.com。d碰定制。sh vi的风俗。sh export PATH=$PATH:/ PATH根据您的设置/重新启动您的计算机,这里我们选择PATH将永远为您欢呼。

#19


0  

For debian distribution, you have to:

对于debian发行版,您必须:

    - 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

#20


-5  

It can be directly added by using the following command:

可以使用以下命令直接添加:

echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc source ~/.zshrc

echo 'export PATH=$PATH:/new/directory' >> ~/。~ / . zshrc zshrc来源