如何在cygwin中设置环境变量?

发布于 2024-11-16 14:22:14 字数 98 浏览 5 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分開簡單 2024-11-23 14:22:15

默认情况下,Cygwin 运行 Bourne shellBash,所以设置变量的命令是不同的。这是您需要的代码:

export MYVAR="somevalue"

export 部分让 shell 知道它是一个环境变量而不是局部变量。

如果您在主目录中输入 ls -a,您应该会看到以下部分或全部文件:

.bashrc
.bash_profile
.profile

为登录 shell 执行 .bash_profile,以及 .bashrc 针对交互式非登录 shell 执行。为了最简单地确保始终设置环境变量,请打开 .bash_profile 并添加文本:

export MYVAR="somevalue"

Your shell with thenexecute .bash_profile every time itstarted, and it将运行此命令。这样您就可以随时访问 MYVAR 变量。如果您没有导出该变量,则只能在您的.bash_profile 文件中访问它。

您可以通过将其值打印到 shell 来检查该变量是否已定义:

echo $MYVAR

您可以使用以下命令删除(取消设置)该变量:

unset $MYVAR

shell 配置文件的简要

说明 顺便说一句,关于 .bashrc.bash_profile.profile,请参阅以下答案:

为了简化配置,我建议从 .bash_profile.bashrc 文件代码>.将其添加到 .bash_profile

if [ -f ${HOME}/.bashrc ]; then
   source ${HOME}/.bashrc
fi

这将从 .bash_profile 加载 .bashrc

如果您这样做,您可以根据需要将以下行放入 .bashrc 中:

export MYVAR="somevalue"

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:

export MYVAR="somevalue"

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:

.bashrc
.bash_profile
.profile

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

export MYVAR="somevalue"

Your shell with then execute .bash_profile every time it starts up, and it will run this command. You will then have the MYVAR variable accessible all of the time. If you didn't export 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:

echo $MYVAR

You can delete (unset) the variable with:

unset $MYVAR

Brief words on shell config files

As an aside, regarding .bashrc vs .bash_profile vs. .profile, see these answers:

For simplicity of configuration, I recommend sourcing your .bashrc file from .bash_profile. Add this to .bash_profile:

if [ -f ${HOME}/.bashrc ]; then
   source ${HOME}/.bashrc
fi

This will load .bashrc from .bash_profile.

If you do this, you can instead put the following line in .bashrc, if you wish:

export MYVAR="somevalue"
绮烟 2024-11-23 14:22:15

在 cygwin 中设置环境变量的最佳方法是创建一个 bash 配置文件,并在每次登录和运行 shell 时执行该配置文件。

在我的 .bash_profile 文件中,这是我的设置

JAVA_HOME = C:/Program Files/Java/jdk1.7.0_51
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

一旦运行 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

JAVA_HOME = C:/Program Files/Java/jdk1.7.0_51
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

Once you run bash, check out echo $JAVA_HOME and you should see the path as output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文