导出 PATH 在 linux 上的 bash shell 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35881004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:21:16  来源:igfitidea点击:

exporting PATH not working in a bash shell on linux

linuxbashshell

提问by Yet A-beyene

For example, I type to the following command:

例如,我输入以下命令:

         # PATH=$PATH:/var/test
         # echo $PATH
           ........./var/test       // working
         # export PATH

Next, I open another bash shell session to test if the export works by typing the following command:

接下来,我打开另一个 bash shell 会话,通过键入以下命令来测试导出是否有效:

         # echo $PATH
          ........      // not working as in I don't see /var/test path

回答by riteshtch

you have set the PATHenvironment variable only for your current bashsession. You need to add the line PATH=$PATH:/var/testinto ~/.bashrcso that it works for any bashshell.

PATH仅为当前bash会话设置了环境变量。您需要将该行添加PATH=$PATH:/var/test到其中,~/.bashrc以便它适用于任何bashshell。

Just run the following command to put it into your rc(run commands) file (rc files contain startup information for a command(initialization)):
echo "PATH=$PATH:/var/test" >> ~/.bashrc

只需运行以下命令将其放入您的rc(运行命令)文件(rc 文件包含命令的启动信息(初始化)):
echo "PATH=$PATH:/var/test" >> ~/.bashrc

More info: https://en.wikipedia.org/wiki/Run_commands
https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc

更多信息:https: //en.wikipedia.org/wiki/Run_commands
https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc

exporting a variable makes it available only in child processes spwaned/started from that bash shell.
As an example:

export使用一个变量使其仅在从该 bash shell 衍生/启动的子进程中可用。
举个例子:

$ export var=abcd
$ sh
$ echo "$var"
abcd
$ exit
$ echo "$var"
abcd
$

shis the child process of bashhence it gets the value of var, since you open a new bashwhich is a different process altogether it does get the PATHvalue.

sh是 的子进程,bash因此它获得 的价值var,因为您打开一个新的bash,这是一个完全不同的过程,它确实获得了PATH价值。