带空格的 Bash 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5819423/
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
Bash variables with spaces
提问by INS
I'm facing the next problem in MinGW shell under windows. I have in my /etc/profile
the expression:
我在 Windows 下的 MinGW shell 中面临下一个问题。我有我/etc/profile
的表达:
export GIT_SSH="/c/Program Files/TortoiseGit/bin/TortoisePlink.exe"
This doesn't work when I use git fetch
on the local repository. But if I do it like this (old DOS way), it works:
当我git fetch
在本地存储库上使用时,这不起作用。但是如果我这样做(旧的 DOS 方式),它会起作用:
export GIT_SSH="/c/Progra~1/TortoiseGit/bin/TortoisePlink.exe"
My question is:
我的问题是:
How can I make it work using spaces in the variable?
如何使用变量中的空格使其工作?
For testing purpose you can simulate something like this (any example is good):
出于测试目的,您可以模拟这样的事情(任何示例都很好):
export VAR="/c/Program Files/TortoiseGit/bin/TortoisePlink.exe"
# and try to execute like this
$VAR
Is there a solution for this (other than the previous mentioned)?
有没有解决方案(除了前面提到的)?
回答by Blagovest Buyukliev
Execute it like this: "$VAR"
. This is one of the most significant gotchas in shell scripting because strings are always substituted literally and any contained spaces are treated as token delimiters rather than as characters of the string. Think of substituting a variable as a kind of code pasting at runtime.
像这样执行它:"$VAR"
. 这是 shell 脚本中最重要的问题之一,因为字符串总是按字面意思替换,并且任何包含的空格都被视为标记分隔符而不是字符串的字符。将变量替换为一种在运行时粘贴的代码。
What really happens when you write $VAR
is that the shell tries to execute the binary /c/Program
with a first argument Files/TortoiseGit/bin/TortoisePlink.exe
.
编写时真正发生的$VAR
是 shell 尝试/c/Program
使用第一个参数执行二进制文件Files/TortoiseGit/bin/TortoisePlink.exe
。
I learned this the hard way by getting a strange syntax error in a big shell script for a particular input. No other languages I can think of can complain for syntax errors if the runtime input contains special characters - but that is the nature of shell scripting since command interpreters like bash and sh interpret the code line by line.
我通过在特定输入的大型 shell 脚本中出现奇怪的语法错误来艰难地了解到这一点。如果运行时输入包含特殊字符,我能想到的其他语言都不会抱怨语法错误 - 但这是 shell 脚本的本质,因为像 bash 和 sh 这样的命令解释器逐行解释代码。
Whenever you expect a string to contain spaces and you don't want to treat it as separate tokens, enclose it in double quotes.
每当您希望字符串包含空格并且不想将其视为单独的标记时,请将其括在双引号中。
回答by Alex
For reference, I solved a similar issue on osx by encapsulating the argument with escaped quotations. This may not be the best solution, but it seems to work.
作为参考,我通过用转义引号封装参数解决了 osx 上的类似问题。这可能不是最好的解决方案,但它似乎有效。
alias sub="\"/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl\""
回答by Eduard Dubilyer
I've solved it by including a backslash to escape the space:
我已经通过包含一个反斜杠来逃避空间来解决它:
/Program Files
becomes /Program\ Files
/Program Files
变成 /Program\ Files
Example:
例子:
export GIT_SSH=/c/Program\ Files/TortoiseGit/bin/TortoisePlink.exe
export GIT_SSH=/c/Program\ Files/TortoiseGit/bin/TortoisePlink.exe
回答by VonC
With Git 2.23 (Q3 2019, eight years later), a GIT_SSH
set to /c/Program\ Files/TortoiseGit/bin/TortoisePlink.exe
will... work (for those still on Windows 7)!
使用 Git 2.23(2019 年第三季度,八年后),一GIT_SSH
组/c/Program\ Files/TortoiseGit/bin/TortoisePlink.exe
将......工作(对于那些仍在 Windows 7 上的人)!
See commit eb7c786(16 Jul 2019) by Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
--in commit a5194d8, 25 Jul 2019)
请参阅Johannes Schindelin ( )提交的 eb7c786(2019 年 7 月 16 日)。(由Junio C Hamano合并-- --在commit a5194d8,2019 年 7 月 25 日)dscho
gitster
mingw
: support spawning programs containing spaces in their namesOn some older Windows versions (e.g. Windows 7), the
CreateProcessW()
function does not really support spaces in its first argument,lpApplicationName
.
But it supports passingNULL
aslpApplicationName
, which makes it figure out the application from the (possibly quoted) first argument oflpCommandLine
.Let's use that trick (if we are certain that the first argument matches the executable's path) to support launching programs whose path contains spaces.
This fixes
git-for-windows/git
issue 692
mingw
: 支持生成名称中包含空格的程序在一些较旧的 Windows 版本(例如 Windows 7)上,该
CreateProcessW()
函数并不真正支持其第一个参数中的空格,lpApplicationName
.
但它支持传递NULL
aslpApplicationName
,这使得它可以从lpCommandLine
.让我们使用这个技巧(如果我们确定第一个参数与可执行文件的路径匹配)来支持启动路径包含空格的程序。
Git 2.24 (Q4 2019) adds a test:
Git 2.24(2019 年第四季度)添加了一个测试:
See commit 71f4960(01 Oct 2019) by Alexandr Miloslavskiy (SyntevoAlex
).
(Merged by Junio C Hamano -- gitster
--in commit 424663d, 09 Oct 2019)
请参阅Alexandr Miloslavskiy ( )提交的 71f4960(2019 年 10 月 1 日)。(由Junio C Hamano合并-- --在提交 424663d 中,2019 年 10 月 9 日)SyntevoAlex
gitster
t0061
: fix test forargv[0]
with spaces (MINGW only)The test was originally designed for the case where user reported that setting
GIT_SSH
to a.bat
file with spaces in path fails on Windows: git-for-windows#692
t0061
:修复argv[0]
带空格的测试(仅限 MINGW)该测试最初是为用户报告在 Windows 上设置路径中带有空格
GIT_SSH
的.bat
文件失败的情况而设计的:git-for-windows#692