简体   繁体   English

git-bash $PATH 无法解析带空格的 windows 目录

[英]git-bash $PATH cannot parse windows directory with space

I would like to use git-bash come with github client on a windows 7 (32bit) machine.我想在 windows 7(32 位)机器上使用带有 github 客户端的 git-bash。 Somehow, the git-bash has already learned the $PATH variables from windows system PATH .不知何故,git-bash 已经从 windows 系统PATH中学习了$PATH变量。 However, I found the $PATH in git-bash did not parse the win system PATH with space correctly.但是,我发现 git-bash 中的$PATH没有正确解析带空格的 win 系统 PATH。

for example the $PATH outputs:例如$PATH输出:

"..../c/Program: No such file or directory" “..../c/Program:没有这样的文件或目录”

Is there anyway to fix it?无论如何要修复它? where is the git-bash script to construct the $PATH for git shell?为 git shell 构造$PATH的 git-bash 脚本在哪里? Thanks谢谢

Why not using absolute path instead of relative path and if a folder of file name contains a space just use "" (quotes).为什么不使用绝对path而不是相对path ,如果文件名的文件夹包含空格,只需使用"" (引号)。

Ex.前任。 cd C:/Users/"My Folder"

Put a backslash (" \\ ") before the space.在空格前放置一个反斜杠(“ \\ ”)。

ex.前任。 cd My\\ Documents

I am having similar problem.我有类似的问题。
After some struggle, i put this on the first line in my C:\\Program Files\\Git\\etc\\profile :经过一番努力,我把它放在我的C:\\Program Files\\Git\\etc\\profile的第一行:

export HOME=`cygpath -u "\\`cygpath -d "$HOMEDRIVE$HOMEPATH"\\`"` export HOME=`cygpath -u "\\`cygpath -d "$HOMEDRIVE$HOMEPATH"\\`"`

and type command echo $HOME in bash will come out /c/Users/NAMEXX~1 , no more space parsing problem for my $HOME or ~/ !并在 bash 中输入命令echo $HOME将出现/c/Users/NAMEXX~1 ,我的$HOME~/不再有空间解析问题!

git-bash runs in a Linux environment. git-bashLinux环境中运行。 As, linux doesn't recognize \\ backslash in its directory path, you need to use / forwardslash .由于 linux 在其目录路径中无法识别\\ backslash ,因此您需要使用/ forwardslash Use "" if you directory path contains spaces.如果目录路径包含空格,请使用""

The answer for me was close to that of Seagal82.对我来说,答案与 Seagal82 接近。 I added the following line at the start of [PATH_TO_GITBASH_INSTALL]\\etc\\profile :我在[PATH_TO_GITBASH_INSTALL]\\etc\\profile的开头添加了以下行:

export HOME="/c/Users/Username\ with\ spaces/"

Then I started a new git bash window and all the paths in $PATH using $HOME got replaced with the anti-slashed space and work correctly.然后我启动了一个新的 git bash 窗口,使用$HOME $PATH所有路径都被反斜杠空间替换并正常工作。

Basically to confirm what is working: For environment variables use export keyword:基本上是为了确认什么是有效的:对于环境变量,使用 export 关键字:

export JAVA_HOME="/c/Program Files/Java/jdkxxxxx"

same for PATH variable etc. PATH 变量等也是如此。

You can put it in your ~/.bashrc .你可以把它放在你的 ~/.bashrc 中。 Then, use variables instead of direct string with spaces然后,使用变量而不是带空格的直接字符串

使用 bash 声明命令 -

$ declare -x PATH="/path with spaces":$PATH

You need to escape non-word-characters with a backslash "\\":您需要使用反斜杠“\\”转义非单词字符:

For Example:例如:

$ cd Program\ Files\ \(x86\)

Don't do that, you can try this instead pass the complete path in the "" (quotes).不要那样做,你可以试试这个,而是在"" (引号)中传递完整的路径。 Example $ cd "C:\\Users\\folder\\OneDrive - folder\\file"示例$ cd "C:\\Users\\folder\\OneDrive - folder\\file"

I have come to appreciate, specifically if in a Windows OS Git Bash shell "setting", the possibility to use "short names" ie the foldername info you get when you run dir /X in a directory and then use that eg for Program Files this would be PROGRA~1 (which contains no spaces anymore).我开始意识到,特别是如果在 Windows OS Git Bash shell“设置”中,可以使用“短名称”,即在目录中运行dir /X时获得的文件夹名称信息,然后将其用于Program Files这将是PROGRA~1 (不再包含空格)。 Then using that shortname in paths or similar definitions within the .bashrc (or similar) files.然后在.bashrc (或类似)文件中的路径或类似定义中使用该短名称。
As an additional info [here] Program Files (x86) shortname would be PROGRA~2作为附加信息 [此处] Program Files (x86)PROGRA~2
I recently discovered this to be the most stable solution for exporting a variable in Git Bash 's .bash_profile我最近发现这是在Git Bash.bash_profile中导出变量的最稳定的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM