领先的美元符号如何影响 Bash 中的单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11966312/
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
How does the leading dollar sign affect single quotes in Bash?
提问by user1206899
I need to pass a string to a program as its argument from the Bash CLI, e.g
我需要将字符串作为参数从 Bash CLI 传递给程序,例如
program "don't do this"
The string may include any character like '$', '\', etc. and I don't want Bash to do any modification. So I think about using single quotes.
该字符串可能包含任何字符,例如'$','\'等,我不希望 Bash 进行任何修改。所以我考虑使用单引号。
However the following does not work:
但是,以下不起作用:
program 'don\'t do this' //escape doesn't work in single quote
While the following two works:
虽然以下两个有效:
program $'dont\'t do this' //seems fine, but any other side effects?
program 'dont'\''do this' //breaking into 3 parts
The first approach seems better in that it acquires less pre modification (put the dollar symbol in front and substitute every \to \\), but I don't know what else the DOLLAR SIGN might do.
第一种方法似乎更好,因为它获得的预修改较少(将美元符号放在前面并替换\为\\),但我不知道美元符号还能做什么。
I've really googled this but I can't find what I need...
我真的用谷歌搜索过这个,但我找不到我需要的东西......
回答by Ignacio Vazquez-Abrams
It causes escape sequences to be interpreted.
它会导致转义序列被解释。
$ echo $'Name\tAge\nBob\t24\nMary\t36'
Name Age
Bob 24
Mary 36
(And SO handles tabs in a goofy manner, so try this one at home)
(SO 以一种愚蠢的方式处理标签,所以在家里试试这个)
回答by Aaron Digulla
Using $as a prefix tells BASH to try to find a variable with that name. $'is a special syntax (fully explained here) which enables ANSI-C string processing. In this case, the single tick isn't "take value verbatim until the next single tick". It should be quite safe to use. The drawbacks are it's BASH only and quite uncommon, so many people will wonder what it means.
使用$作为前缀告诉Bash试图找到具有该名称的变量。$'是一种特殊的语法(这里有完整的解释),它支持 ANSI-C 字符串处理。在这种情况下,单个刻度不是“在下一个单个刻度之前逐字取值”。使用起来应该是很安全的。缺点是它只是 BASH 并且非常不常见,所以很多人会想知道它意味着什么。
The better way is to use single quotes. If you need a single quote in a string, you need to replace it with '\''. This ends the previous single quoted string, adds a single quote to it (\') and then starts a new single quoted string. This syntax works with any descendant of the Bourne shell, it's pretty easy to understand and most people quickly recognize the pattern.
更好的方法是使用单引号。如果字符串中需要单引号,则需要将其替换为'\''. 这会结束前一个单引号字符串,为其添加单引号 ( \'),然后开始一个新的单引号字符串。这种语法适用于Bourne shell 的任何后代,它很容易理解,大多数人很快就能认出这种模式。
The alternative is to replase each single tick with '"'"'which translates to "terminate current single quoted string, append double quoted string which contains just a single tick, restart single quoted string". This avoid the escape character and looks nicely symmetric. It also works the other way around if you need a double quote in a double quoted string: "'"'".
另一种方法是替换每个单'"'"'引号,这意味着“终止当前的单引号字符串,附加只包含一个单引号的双引号字符串,重新启动单引号字符串”。这避免了转义字符并且看起来很好对称。它也可以,如果你需要在双引号字符串双引号周围的其他方法:"'"'"。
回答by David Kubí?ek
You won't find a faster or more efficient way than:
您找不到比以下更快或更有效的方法:
eval RESULT=$\'$STRING\'
For one, this is the kind of thing that evalis there for, and you avoid the crazy cost of forking subprocess all over the place, as the previous answers seem to suggest. Your example:
首先,这就是eval的用途,并且您可以避免在整个地方分叉子流程的疯狂成本,正如之前的答案所暗示的那样。你的例子:
$ foo='\u25b6'
$ eval bar=$\'$foo\'
$ echo "$bar"
?

