[英]Git Bash doesn't see my PATH
When I use Git Bash (on Windows), I cannot run any executable without specifying its full path, although it is located in a folder which is in my PATH variable.当我使用 Git Bash(在 Windows 上)时,我无法在不指定完整路径的情况下运行任何可执行文件,尽管它位于我的 PATH 变量中的文件夹中。 Looks like bash doesn't recognize it.
看起来 bash 无法识别它。 Why?
为什么? Can I fix it?
我可以修吗?
Got it.知道了。 As a Windows user, I'm used to type executable names without extensions.
作为 Windows 用户,我习惯于键入不带扩展名的可执行文件名。 In my case, I wanted to execute a file called
cup.bat
.就我而言,我想执行一个名为
cup.bat
的文件。 In a Windows shell, typing cup
would be enough.在 Windows shell 中,输入
cup
就足够了。 Bash doesn't work this way, it wants the full name. Bash 不是这样工作的,它需要全名。 Typing
cup.bat
solved the problem.输入
cup.bat
解决了这个问题。 (I wasn't able to run the file though, since apparently bash couldn't understand its contents) (虽然我无法运行该文件,因为显然 bash 无法理解其内容)
One more reason to switch to posh-git..切换到 posh-git 的另一个原因..
Thanks @Tom for pointing me to the right direction.感谢@Tom 为我指明了正确的方向。
Maybe bash doesn't see your Windows path.也许 bash 没有看到您的 Windows 路径。 Type
env|grep PATH
in bash to confirm what path it sees.在 bash 中输入
env|grep PATH
以确认它看到的路径。
Following @Daniel's comment and thanks to @Tom's answer, I found out that Git bash was indeed using the PATH but not the latest paths I recently installed.遵循@Daniel 的评论并感谢@Tom 的回答,我发现 Git bash 确实使用了 PATH 而不是我最近安装的最新路径。 To work around this problem, I added a file in my home (windows) directory named:
为了解决这个问题,我在我的 home (windows) 目录中添加了一个名为:
.bashrc
and the content as follow:内容如下:
PATH=$PATH:/c/Go/bin
because I was installing Go and this path contained the executable go.exe
Now Git bash was able to recognize the command:因为我正在安装 Go 并且此路径包含可执行文件
go.exe
现在 Git bash 能够识别命令:
go
Perhaps just a system reboot would have been enough in my case, but I'm happy that this solution work in any case.也许在我的情况下只需重新启动系统就足够了,但我很高兴这个解决方案在任何情况下都有效。
While you are installing Git, you can select the option shown below, it'll help you to set the path automatically.在安装 Git 时,您可以选择如下所示的选项,它会帮助您自动设置路径。
Its worked out for me :)它对我有用:)
Create a file in C:\\Users\\USERNAME which is called config.bashrc, containing:在 C:\\Users\\USERNAME 中创建一个名为 config.bashrc 的文件,其中包含:
PATH=$PATH:/c/Program\ Files\ \(x86\)/Application\ with\ space
Now move the file on the command line to the correct location:现在将命令行上的文件移动到正确的位置:
mv config.bashrc .bashrc
将新值添加到 PATH 后重新启动计算机。
Old question but it can help someone else.老问题,但它可以帮助别人。
I've changed my PATH user wide, after that I've just logoff and login again.我已经更改了我的 PATH 用户范围,之后我刚刚注销并再次登录。
That is it!这就对了!
git bash
loaded the new PATH value correctly. git bash
正确加载了新的 PATH 值。
我可以确认重新启动系统将确保在windows中的环境变量中设置的PATH被git拾取并且没有其他自动方式。
I know it is an old question but there's two type of environment variables.我知道这是一个老问题,但有两种类型的环境变量。 The one owned with User and the one system wide.
一种是用户拥有的,一种是系统范围内的。 Depending how do you open git bash (with user privilege or with administrator privilege) the environment variable PATH used can be from you User variables or from System variables.
根据您如何打开 git bash(具有用户权限或具有管理员权限),使用的环境变量 PATH 可以来自您的用户变量或来自系统变量。 See below:
见下文:
as said in a previous answer, check with the command env|grep PATH
to see which one you are using and update your variable accordingly.如上一个答案中所述,请使用命令
env|grep PATH
来查看您正在使用哪个并相应地更新您的变量。 BTW, no need to reboot the system.顺便说一句,无需重新启动系统。 Just close and reopen the git bash
只需关闭并重新打开 git bash
In case your git-bash
's PATH
presents but not latest and you don't want a reboot but regenerate your PATH
s, you can try the following:如果您的
git-bash
的PATH
出现但不是最新的,并且您不想重新启动但重新生成您的PATH
,您可以尝试以下操作:
cmd.exe
, powershell.exe
, and git-bash.exe
and reopen one cmd.exe window from the Start Menu or Desktop context.cmd.exe
、 powershell.exe
和git-bash.exe
并从开始菜单或桌面上下文重新打开一个 cmd.exe 窗口。PATH
, you may also need to open one privileged cmd window.PATH
,您可能还需要打开一个特权 cmd 窗口。PATH
env is updated.PATH
env 是否已更新。 Please note that the terminal in IntelliJ IDEA is probably a login shell or some other kind of magic, so PATH
in it may won't change until you restart IDEA .PATH
可能不会更改。Windows Explorer
process as well and retry the steps above.Windows Explorer
进程并重试上述步骤。 Note: This doesn't work with all Windows versions, and open cmd.exe
anywhere other than the Start Menu or Desktop context menu may not work , tested with my 4 computers and 3 of them works.注意:这不适用于所有 Windows 版本,并且在开始菜单或桌面上下文菜单以外的任何地方打开
cmd.exe
可能无法正常工作,在我的 4 台计算机上进行了测试,其中 3 台可以正常工作。 I didn't figure out why this works, but since the PATH
environment variable is generated automatically when I login and logout, I'd not to mess up that variable with variable concatenation.我不知道为什么会这样,但是由于
PATH
环境变量是在我登录和注销时自动生成的,因此我不会用变量串联来弄乱该变量。
It seems the root cause here is Git Bash not able to always parse the variable %USERPROFILE% correctly.这里的根本原因似乎是 Git Bash 无法始终正确解析变量 %USERPROFILE%。 Instead of making it relative to C:\\Users\\\\ it gets the value C:\\Windows\\System 32\\systemprofile\\ After changing this to a fully qualified address, it Works, and even if I set it back afterwards, Git Bash still has the correct PATH for some reason.
而不是使它相对于 C:\\Users\\\\ 它获得值 C:\\Windows\\System 32\\systemprofile\\由于某种原因有正确的路径。
I meet this problem when I try to use mingw to compile the xgboost lib in Win10.我在Win10中尝试使用mingw编译xgboost lib时遇到了这个问题。 Finally I found the solution.
最后我找到了解决方案。
Create a file named as .bashrc in your home directory (usually the C:\\Users\\username).在您的主目录(通常是 C:\\Users\\username)中创建一个名为 .bashrc 的文件。 Then add the path to it.
然后添加路径。 Remember to use quotes if your path contains blank, and remember to use /c/ instead of C:/
如果您的路径包含空白,请记住使用引号,并记住使用 /c/ 而不是 C:/
For example:例如:
PATH=$PATH:"/c/Program Files/mingw-w64/x86_64-7.2.0-posix-seh-rt_v5-rev1/mingw64/bin" PATH=$PATH:"/c/Program Files/mingw-w64/x86_64-7.2.0-posix-seh-rt_v5-rev1/mingw64/bin"
On Windows 10, just uninstall git and install it again.在 Windows 10 上,只需卸载 git 并重新安装即可。 It will set the environment variable automatically for you.
它将自动为您设置环境变量。 I had removed the environment variable by mistake and I couldn't use git inside my IDE.
我错误地删除了环境变量,无法在 IDE 中使用 git。 Reinstalling git fixed this issue.
重新安装 git 解决了这个问题。
For me the most convenient was to: 1) Create directory "bin" in the root of C: drive 2) Add "C:/bin;"对我来说最方便的是:1) 在 C: 驱动器的根目录中创建目录“bin” 2) 添加“C:/bin;” to PATH in "My Computer -> Properties -> Environemtal Variables"
到“我的电脑 -> 属性 -> 环境变量”中的 PATH
I've run into a stupid mistake on my part.我遇到了一个愚蠢的错误。 I had a systems wide and a user variable path set for my golang workspace on my windows 10 machine.
我在 Windows 10 机器上为我的 golang 工作区设置了系统范围和用户变量路径。 When I removed the redundant systems variable pathway and logged off and back on, I was able to call .exe files in bash and call go env with success.
当我删除冗余系统变量路径并注销并重新登录时,我能够在 bash 中调用 .exe 文件并成功调用 go env。
Although OP has been answered this is another problem that could keep bash from seeing your pathways.尽管 OP 已得到回答,但这是另一个可能使 bash 无法看到您的路径的问题。 I just tested bash again with this problem and it does seem to give a conflict of some sort that blocks bash from following either of the paths.
我刚刚用这个问题再次测试了 bash,它似乎确实产生了某种冲突,阻止了 bash 遵循任一路径。
In my case It happened while installing heroku cli and git bash, Here is what i did to work.在我的情况下,它发生在安装 heroku cli 和 git bash 时,这是我所做的工作。
got to this location到了这个位置
C:\\Users\\<username here>\\AppData\\Local
and delete the file in my case heroku folder.并删除我的案例 heroku 文件夹中的文件。 So I deleded folder and run cmd.
所以我删除文件夹并运行cmd。 It is working
这是工作
Git bash terminal on windows operating system have capability to read system path / user path and run the apps but it might miss in following cases Windows 操作系统上的 Git bash 终端能够读取系统路径/用户路径并运行应用程序,但在以下情况下可能会丢失
Use Case - 1 :用例 - 1:
Restart terminals should work and use below command to verify your path重新启动终端应该可以工作并使用以下命令来验证您的路径
env|grep PATH
Use Case - 2 :用例 - 2:
let me explain with an example docker
won't work directly on the git bash terminal, for such apps MSYS is converting it origin path.让我用一个例子来解释
docker
不能直接在 git bash 终端上工作,对于这样的应用程序,MSYS 正在转换它的原始路径。 for such issues you might need to tell your terminal ignore path conversion using command MSYS_NO_PATHCONV=1
and proceed with your actual execution command, say for example docker --help
should be like below对于此类问题,您可能需要使用命令
MSYS_NO_PATHCONV=1
告诉您的终端忽略路径转换并继续执行您的实际执行命令,例如docker --help
应如下所示
MSYS_NO_PATHCONV=1 MSYS_NO_PATHCONV=1 docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
717d12b9fe5211f0189ccbed0ba056ca242647812627682d0149ede29af472a4
Use Case - 3 : let me explain with an example az
cli, ideally az cli install in windows as az.cmd
which is added to system/user path.用例 - 3:让我用一个例子来解释
az
cli,理想情况下 az cli 在 windows 中作为az.cmd
安装,它被添加到系统/用户路径。 on windows operating system either powershell or command prompt recognize az.cmd
as az
but git bash won't understand it - so you have use alias to avoid the confusion say alias az='az.cmd'
and then execute az --help
will work for you在 Windows 操作系统上,powershell 或命令提示符将
az.cmd
识别为az
但 git bash 不会理解它 - 所以你必须使用别名来避免混淆,说alias az='az.cmd'
然后执行az --help
将为你工作
Don't escape (\\) special characters when editing/adding to your $PATH variable.编辑/添加到 $PATH 变量时不要转义 (\\) 特殊字符。 For example, an application directory in program files would look like:
PATH=$PATH:/c/Program Files (x86)/random/application
例如,程序文件中的应用程序目录如下所示:
PATH=$PATH:/c/Program Files (x86)/random/application
Don't do this:不要这样做:
PATH=$PATH:/c/Program\\ Files\\ \\\\(x86\\\\)/random/application/
Hope this helps.希望这可以帮助。
For those of you who have tried all the above mentioned methods including Windows system env.对于那些尝试过上述所有方法的人,包括 Windows 系统环境。 variables, .bashrc, .bashprofile, etc. AND can see the correct path in 'echo $PATH' ... I may have a solution for you.
变量、.bashrc、.bashprofile 等。并且可以在 'echo $PATH' 中看到正确的路径......我可能会为您提供解决方案。
suppress the errors using exec 2> /dev/null使用exec 2> /dev/null抑制错误
My script runs fine but was throwing 'command not found' or 'No directory found' errors even though, as far as I can tell, the paths were flush.我的脚本运行良好,但抛出“未找到命令”或“未找到目录”错误,尽管据我所知,路径已刷新。 So, if you suppress those errors (might have to also add 'set +e'), than it works properly.
因此,如果您抑制这些错误(可能还必须添加“set +e”),则它可以正常工作。
Create a User variable named Path and add as value %Path%, from what I noticed Git Bash only sees User Variables and not System Variables.创建一个名为 Path 的用户变量并将其添加为值 %Path%,从我注意到 Git Bash 只能看到用户变量而不是系统变量。 By doing the mentioned procedure you'll expose your System Variable in the User Variables.
通过执行上述过程,您将在用户变量中公开您的系统变量。
In Windows 7 Path Environment Variables I just add at the end of System Variable path在 Windows 7 路径环境变量中,我只是在系统变量路径的末尾添加
\\;C:\\Program Files\\Git\\bin \\;C:\\Program Files\\Git\\bin
and it works now!现在可以使用了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.