在字符串外带有 $ 字符的 Bash 回显

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

Bash echo with an $ character outside the string

linuxbashecho

提问by u351545

Can anyone explain what is the difference between

谁能解释一下有什么区别

echo $"Starting $CMD" 

and

echo "String $CMD"

They seem to look the same.

他们似乎看起来一样。

回答by larsks

Look up the QUOTINGsection of the bashman page:

查看手册页的QUOTING部分bash

Words of the form $'string'are treated specially. The word expands to string, with backslash- escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

  • \a alert (bell)
  • \b backspace
  • \e an escape character
  • \f form feed
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab
  • \ backslash
  • \' single quote
  • \nnn the eight-bit character whose value is the octal value nnn (one to three digits)
  • \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
  • \cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

表格$'string'中的词被特殊处理。单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。反斜杠转义序列(如果存在)按如下方式解码:

  • \a 警报(响铃)
  • \b 退格
  • \e 转义字符
  • \f 换页
  • \n 换行
  • \r 回车
  • \t 水平制表符
  • \v 垂直制表符
  • \ 反斜杠
  • \' 单引号
  • \nnn 八位字符,其值为八进制值 nnn(一到三位数)
  • \xHH 八位字符,其值为十六进制值 HH(一个或两个十六进制数字)
  • \cx 一个 control-x 字符

扩展结果是单引号的,就好像美元符号不存在一样。

And note the follow description double quoted strings preceded by $($"string"):

并注意以下以$( $"string")开头的双引号字符串说明:

A double-quoted string preceded by a dollar sign ($) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

以美元符号 ($) 开头的双引号字符串将使字符串根据当前区域设置进行翻译。如果当前语言环境是 C 或 POSIX,则忽略美元符号。如果字符串被翻译和替换,则替换是双引号。

回答by kojiro

$""is a form of quoting that is used for language translation. If you invoke bash with the -Dflag it will print out all such strings. When the locale is Cor POSIX, this form of quotation doesn't do anything.

$""是一种用于语言翻译的引用形式。如果您使用-D标志调用 bash,它将打印出所有此类字符串。当语言环境是Cor 时POSIX,这种形式的引用不做任何事情。

These strings are used by gettextto look up appropriate translations. For example, if your script has the string $"Hello, World"and you have properly installed MO files for translating that string into, say, French, you could execute your script this way:

这些字符串用于gettext查找适当的翻译。例如,如果您的脚本具有字符串$"Hello, World"并且您已正确安装用于将该字符串翻译成法语的 MO 文件,您可以通过以下方式执行您的脚本:

LANGUAGE=fr_FR ./yourscript

and expect all instances of $"Hello, World"to be output as Bonjour, Tout le Monde(assuming that is what is actually in your translation file).

并期望 的所有实例都$"Hello, World"输出为Bonjour、Tout le Monde(假设这就是您的翻译文件中的实际内容)。

Translation is not magic or automatic, so you must provide the translation engine with whatever translation strings it doesn't already have.

翻译不是魔术或自动的,因此您必须向翻译引擎提供它尚未提供的任何翻译字符串

(PS - the guide I linked to does mention this security bug, but you may miss it if you skim, so I highlight it again here.)

(PS - 我链接的指南确实提到了这个安全漏洞,但如果你略读,你可能会错过它,所以我在这里再次强调它。)