如何在cygwin中设置环境变量?
在linux中我会去:
setenv -p MYVAR "somevalue"
但这似乎在cygwin中不起作用。
In linux I would go:
setenv -p MYVAR "somevalue"
But this doesn't seem to work in cygwin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,Cygwin 运行 Bourne shell 或 Bash,所以设置变量的命令是不同的。这是您需要的代码:
export
部分让 shell 知道它是一个环境变量而不是局部变量。如果您在主目录中输入
ls -a
,您应该会看到以下部分或全部文件:为登录 shell 执行
.bash_profile
,以及.bashrc
针对交互式非登录 shell 执行。为了最简单地确保始终设置环境变量,请打开.bash_profile
并添加文本:Your shell with thenexecute
.bash_profile
every time itstarted, and it将运行此命令。这样您就可以随时访问MYVAR
变量。如果您没有导出
该变量,则只能在您的.bash_profile
文件中访问它。您可以通过将其值打印到 shell 来检查该变量是否已定义:
您可以使用以下命令删除(取消设置)该变量:
shell 配置文件的简要
说明 顺便说一句,关于
.bashrc
与.bash_profile
与.profile
,请参阅以下答案:.bash_profile
和之间的区别>.bashrc
.profile
和.bash_profile
为了简化配置,我建议从
.bash_profile.bashrc
文件代码>.将其添加到.bash_profile
:这将从
.bash_profile
加载.bashrc
。如果您这样做,您可以根据需要将以下行放入
.bashrc
中:By default Cygwin is running the Bourne shell or Bash, so the command to set a variable is different. This is the code you need:
The
export
part lets the shell know that it is an environment variable instead of a local variable.If you type
ls -a
in your home directory, you should see some or all of the following files:.bash_profile
is executed for login shells, and.bashrc
is executed for interactive non-login shells. To most simply ensure that your environment variable is always set, open up.bash_profile
and add the text:Your shell with then execute
.bash_profile
every time it starts up, and it will run this command. You will then have theMYVAR
variable accessible all of the time. If you didn'texport
the variable, it would only be accessible within your.bash_profile
file.You can check that this variable is defined by printing its value to your shell:
You can delete (unset) the variable with:
Brief words on shell config files
As an aside, regarding
.bashrc
vs.bash_profile
vs..profile
, see these answers:.bash_profile
and.bashrc
.profile
and.bash_profile
For simplicity of configuration, I recommend sourcing your
.bashrc
file from.bash_profile
. Add this to.bash_profile
:This will load
.bashrc
from.bash_profile
.If you do this, you can instead put the following line in
.bashrc
, if you wish:在 cygwin 中设置环境变量的最佳方法是创建一个 bash 配置文件,并在每次登录和运行 shell 时执行该配置文件。
在我的 .bash_profile 文件中,这是我的设置
一旦运行 bash,请检查 echo $JAVA_HOME,您应该会看到路径作为输出。
The best way to set up environment variables in cygwin is to create a bash profile and execute that profile everytime you login and run the shell.
In my .bash_profile file , this is the setting I have
Once you run bash, check out echo $JAVA_HOME and you should see the path as output.