击回声字符串外的$字符(Bash echo with an $ character outside

2019-06-26 19:19发布

谁能解释之间有什么区别

echo $"Starting $CMD" 

echo "String $CMD"

他们似乎看起来是一样的。

Answer 1:

Look up the QUOTING section of the bash man page:

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.

And note the follow description double quoted strings preceded by $ ($"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.



Answer 2:

$""是引用一个用于语言翻译的一种形式。 如果调用的bash与-D标志将打印出所有这样的字符串。 当语言环境是CPOSIX ,这种形式的报价并没有做任何事情。

这些字符串使用gettext查找合适的翻译。 例如,如果你的脚本有字符串$"Hello, World" ,你已经正确安装MO文件翻译该字符串到,比如说,法国,你可以执行你的脚本是这样的:

LANGUAGE=fr_FR ./yourscript

和期望的所有实例$"Hello, World"是为卓悦输出,招徕世界报 (假设这就是实际上是在你的翻译文件)。

翻译不是魔术或自动的,所以你必须提供与任何翻译字符串它已经没有翻译引擎 。

(PS -我挂导确实提到这个安全漏洞 ,但你可能会错过它,如果你脱脂,所以我强调一遍在这里。)



文章来源: Bash echo with an $ character outside the string
标签: linux bash echo