在 git bash 中设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34169721/
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-09 03:45:39  来源:igfitidea点击:

Set an environment variable in git bash

gitgit-bash

提问by bier hier

When I punch from the windows gitbash command line:

当我从 windows gitbash 命令行打孔时:

set $HOME = c

and do :

并做:

echo $HOME

It does not set it to c? How can I change/set the value of an environment variable?

它没有将其设置为c? 如何更改/设置环境变量的值?

回答by chepner

A normal variable is set by simply assigning it a value; note that no whitespace is allowed around the =:

普通变量通过简单地赋值来设置;请注意, 周围不允许有空格=

HOME=c

An environment variable is a regular variable that has been marked for export to the environment.

环境变量是已标记为导出到环境的常规变量。

export HOME
HOME=c

You can combine the assignment with the exportstatement.

您可以将赋值与export语句结合起来。

export HOME=c

回答by Alexandr

If you want to set environment variables permanently in Git-Bash, you have two options:

如果你想在 Git-Bash 中永久设置环境变量,你有两个选择:

  1. Set a regular Windows environment variable. Git-bash gets all existing Windows environment variables at startupp.

  2. Set up env variables in .bash_profilefile.

  1. 设置一个常规的 Windows 环境变量。Git-bash 在启动时获取所有现有的 Windows 环境变量。

  2. .bash_profile文件中设置环境变量。

.bash_profileis by default located in a user home folder, like C:\users\userName\git-home\.bash_profile. You can change the path to the bash home folder by setting HOMEWindows environment variable.

.bash_profile默认情况下位于用户主文件夹中,例如C:\users\userName\git-home\.bash_profile. 您可以通过设置HOMEWindows 环境变量来更改 bash 主文件夹的路径。

.bash_profilefile uses the regular Bash syntax and commands

.bash_profile文件使用常规的 Bash 语法和命令

# Export a variable in .bash_profile
export DIR=c:\dir
# Nix path style works too
export DIR=/c/dir

# And don't forget to add quotes if a variable contains whitespaces
export ANOTHER_DIR="c:\some dir"

Read more information about Bash configurations files.

阅读有关Bash 配置文件的更多信息。

回答by kyleus

Creating a .bashrc file in your home directory also works. That way you don't have to copy your .bash_profile every time you install a new version of git bash.

在您的主目录中创建 .bashrc 文件也有效。这样您就不必每次安装新版本的 git bash 时都复制 .bash_profile。