执行包含 Git-Bash 空格的 "cmd" 命令?
Executing Command with "cmd" That Contains Spaces Through Git-Bash?
我正在尝试使用 cmd 执行带有 git-bash 的命令。我需要传递命令才能执行。问题是我需要将批处理文件的路径传递给命令并且它有空格。我似乎无法以一种它能理解的方式将引号传递给 "cmd"。
cmd //C "c:\Program Files (x86)\another path\file.bat args && echo done"
那会给我一个错误:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
如果我尝试使用单引号,它会将文字反斜杠传递给 cmd。
cmd //C '"c:\Program Files (x86)\another path\file.bat" args && echo done'
这会给我同样的错误但显示反斜杠:
'\"c:\Program Files (x86)\another path\file.bat\"' is not recognized as an internal or external command, operable program or batch file.
对于 CMD /C,请参阅“Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args”
尝试在 double-quotes
内使用 Unix-style 路径
$ CMD //C "/c/Program Files (x86)/Opera/New folder/a.bat"
(没有&& echo done
)
使用 git 版本 2.11.0.windows.1 在 Windows 10.
上测试
注意:对于简单的命令,您不需要 CMD /C
从 git bash 会话中执行 bat。在这种情况下,您可以转义空格和括号:
vonc@vonc MINGW64 /c/Users/vonc
$ /c/Program\ Files\ \(x86\)/Test/New\ folder/a.bat
OP 在评论中补充:
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 want echo $LIB
to print nothing after executing the command.
echo %LIB%
很棘手,因为 CMD 会话将使用来自 Git 的回显,而不是 echo
内置 CMD shell 命令。
但是 chaining multiple command in a CMD
(此处为 Git bash 会话改编)完成了:
cmd //V //C "C:/path to/vcvarsall.bat&&set myvar"
set myvar
将显示 bat 脚本为 myvar
设置的值。
然而,回到 bash 会话,echo ${myvar}
不会显示任何内容。
另一种简单的方法是导航到包含该文件的目录(使用 cd 命令),然后 运行 命令并传递文件名。
我正在尝试使用 cmd 执行带有 git-bash 的命令。我需要传递命令才能执行。问题是我需要将批处理文件的路径传递给命令并且它有空格。我似乎无法以一种它能理解的方式将引号传递给 "cmd"。
cmd //C "c:\Program Files (x86)\another path\file.bat args && echo done"
那会给我一个错误:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
如果我尝试使用单引号,它会将文字反斜杠传递给 cmd。
cmd //C '"c:\Program Files (x86)\another path\file.bat" args && echo done'
这会给我同样的错误但显示反斜杠:
'\"c:\Program Files (x86)\another path\file.bat\"' is not recognized as an internal or external command, operable program or batch file.
对于 CMD /C,请参阅“Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args”
尝试在 double-quotes
内使用 Unix-style 路径$ CMD //C "/c/Program Files (x86)/Opera/New folder/a.bat"
(没有&& echo done
)
使用 git 版本 2.11.0.windows.1 在 Windows 10.
上测试注意:对于简单的命令,您不需要 CMD /C
从 git bash 会话中执行 bat。在这种情况下,您可以转义空格和括号:
vonc@vonc MINGW64 /c/Users/vonc
$ /c/Program\ Files\ \(x86\)/Test/New\ folder/a.bat
OP 在评论中补充:
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 的回显,而不是 echo
内置 CMD shell 命令。
但是 chaining multiple command in a CMD
(此处为 Git bash 会话改编)完成了:
cmd //V //C "C:/path to/vcvarsall.bat&&set myvar"
set myvar
将显示 bat 脚本为 myvar
设置的值。
然而,回到 bash 会话,echo ${myvar}
不会显示任何内容。
另一种简单的方法是导航到包含该文件的目录(使用 cd 命令),然后 运行 命令并传递文件名。