zsh 或 bash 有方便英文文本的引号吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12884902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:32:40  来源:igfitidea点击:

Do zsh or bash have quotes that are convenient for English text?

bashescapingquoteszsh

提问by Owen

If I use single quotes, words with apostrophes ("don't") are annoying to escape:

如果我使用单引号,带有撇号(“不要”)的单词很难转义:

'Don'"'"'t do that'

If I use double quotes, dollar signs and exclamation points trip it up:

如果我使用双引号,美元符号和感叹号会绊倒它:

"It cost like $1000\!"

Is there another kind of quoting I can use?

我可以使用另一种引用吗?

edit: I should also add that I would like to pass this string directly as a command line argument, rather than storing it in a variable. To this end I tried, using DigitalRoss's solution,

编辑:我还应该补充一点,我想直接将此字符串作为命令行参数传递,而不是将其存储在变量中。为此,我尝试使用 DigitalRoss 的解决方案,

$ echo "$(cat << \EOF 
Don't $worry be "happy".
EOF)"

but get

但得到

dquote cmdsubst> 

after hitting enter :/ . So at this point ZyX's suggestion of setopt rcquoteslooks to be the most convenient.

按回车后:/。所以此时ZyX的建议setopt rcquotes看起来是最方便的。

回答by ZyX

With zsh you may do

使用 zsh 你可以

setopt rcquotes

. Then ASCII apostrophes are escaped just like this:

. 然后像这样转义 ASCII 撇号:

echo 'Don''t'

. Or setup your keymap to be able to enter UTF apostrophes, they have no issues with any kind of quotes (including none) in any shell:

. 或者将您的键盘映射设置为能够输入 UTF 撇号,它们在任何 shell 中都没有任何类型的引号(包括无引号)的问题:

echo 'Don't'

. Third works both for zsh and bash:

. 第三个适用于 zsh 和 bash:

echo $'Don\'t'

.

.

Neither first nor third can narrow down quote to a single character, but they are still less verbose for non-lengthy strings then heredocs suggested above. With zsh you can do this by using custom accept-line widget that will replace constructs like 'Don't'with 'Don'\''t'. Requires rather tricky regex magic that I can write only in perl; and is probably not the best idea as I can't pretend I can cover all possible cases before they will hit. It won't in any case touch any scripts.

first 和third 都不能将引用范围缩小到单个字符,但是对于非长字符串,它们仍然比上面建议的heredocs 更简洁。随着zsh中,您可以通过使用自定义接受在线小工具,将取代结构,如这样做'Don't''Don'\''t'。需要相当棘手的正则表达式魔术,我只能用 perl 编写;并且可能不是最好的主意,因为我不能假装我可以在它们发生之前涵盖所有可能的情况。它在任何情况下都不会触及任何脚本。

回答by DigitalRoss

I like the direction Zsolt Botykaiis going. Here is an example that works with any Posix shell. (I also verified that it survived its paste into the SO server.)

我喜欢Zsolt Botykai的发展方向。这是一个适用于任何 Posix shell 的示例。(我还验证了它在粘贴到 SO 服务器中后仍然存在。)

$ read -r x << \EOF
Don't $worry be "happy".
EOF
$ echo $x
Don't $worry be "happy".

The things that make this work;

使这项工作发挥作用的东西;

  • -rwill make \not be magic
  • the \EOFinstead of just EOFmakes $not be magic
  • -r\不会是魔术
  • \EOF,而不仅仅是EOF品牌$不是魔术

回答by Zsolt Botykai

If you want to assign a quoted text to a variable, you still can use heredocs, like (and it can be a multiline text too):

如果要将带引号的文本分配给变量,您仍然可以使用heredocs,例如(它也可以是多行文本):

read -r -d '' VAR <<'ENDOFVAR'
This "will be" a 'really well' escaped text. Or won't.
ENDOFVAR

回答by mj41

Bash syntax $'string'is another quoting mechanism which allows ANSI C-like escape sequences and do expansion to single-quoted version.

Bash 语法$'string'是另一种引用机制,它允许类似 ANSI C 的转义序列并扩展到单引号版本。

$> echo $'Don\'t $worry be "happy".'
Don't $worry be "happy".

See https://stackoverflow.com/a/16605140/149221for more details.

有关更多详细信息,请参阅https://stackoverflow.com/a/16605140/149221