用"cmd"执行命令.通过Git-Bash包含空间?
我正在尝试使用cmd使用git-bash执行命令.我需要传递命令才能执行.问题是我需要将批处理文件的路径传递到命令中,并且它具有空格.我似乎无法以能理解的方式将引号传递给"cmd".
I'm trying to execute a command with git-bash using cmd. I need to pass the command for it to execute. The problem is I need to pass the path of the batch file into the command and it has spaces. I can't seem to be able to pass quotes to "cmd" in a way that it will understand it.
cmd//C"c:\ Program Files(x86)\ another path \ file.bat args&& echo done"
那会给我一个错误:
"C:\ Program"未被识别为内部或外部命令,可操作程序或批处理文件.
如果我尝试使用单引号,则会将文字反斜杠传递给cmd.
If I try using single quotes then it passes a literal backslash to cmd.
cmd//C'"c:\ Program Files(x86)\另一个路径\ file.bat" args&回声完成"
哪个错误会给我同样的错误,但是会显示反斜杠:
Which will give me the error the same error but show the backslashes:
'\"c:\ Program Files(x86)\另一个路径\ file.bat \"'不被识别为内部或外部命令,可操作程序或批处理文件.
对于CMD/C,请参阅"针对Windows的Git中的Bash:使用带有args的CMD.exe/C运行命令时很奇怪"
For CMD /C, see "Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args"
尝试在双引号内使用Unix风格的路径
Try using a Unix-style path, within double-quotes
$ CMD //C "/c/Program Files (x86)/Opera/New folder/a.bat"
(没有完成&&&&&&&& && amp;&&&&&&&&&&&&&&&&&&&&&&&&&&'<"<<<>)
(no && echo done
)
在Windows 10上使用git版本2.11.0.windows.1测试.
Tested with git version 2.11.0.windows.1 on Windows 10.
注意:对于简单的命令,不需要 CMD/C
即可从git bash会话执行bat.在这种情况下,您可以转义空格和括号:
Note: For simple commands, you don't need a CMD /C
to execute a bat from a git bash session. In that case, you can escape spaces and parenthesis:
vonc@vonc MINGW64 /c/Users/vonc
$ /c/Program\ Files\ \(x86\)/Test/New\ folder/a.bat
OP在注释中添加:
我希望环境变量仅存在于
cmd
的一个实例上.我不希望它实际上在bash中设置环境变量.
这就是为什么"&&
"之所以必要的原因,因为另一个命令将依赖于这些变量.这是我唯一需要依赖它们的命令.cmd/C"C:/vcvarsall.bat的路径&& echo%LIB%"
应打印由批处理文件设置的env变量.
然后在git-bash中,我希望echo $ LIB
在执行命令后不显示任何内容.
I want the environment variables to only exist on the one instance of
cmd
. I don't want it to actually set the environment variables in bash.
Which is why the "&&
" is necessary, as the other command would rely on those variables. That's the only command I need to rely on them for.cmd /C "C:/path to/vcvarsall.bat && echo %LIB%"
should print the env variable set by the batch file.
Then in git-bash I wantecho $LIB
to print nothing after executing the command.
echo%LIB%
非常棘手,因为CMD会话将使用来自Git的回显,而不是内置CMD shell命令的 echo
.但是在 CMD
中链接多个命令(此处适用于Git bash会话)是通过以下方式完成的::
echo %LIB%
is tricky because the CMD session would use the echo from Git, instead of the echo
builtin CMD shell command.
But chaining multiple command in a CMD
(adapted here for a Git bash session) is done with:
cmd //V //C "C:/path to/vcvarsall.bat&&set myvar"
set myvar
将显示bat脚本为 myvar
设置的值.
然而,回到bash会话中, echo $ {myvar}
不会显示任何内容.
set myvar
will display the value set for myvar
by the bat script.
And yet, back in the bash session, an echo ${myvar}
would not display anything.