像$"Hello World."这样封装的字符串。

3

我刚刚在Centos 6.5上阅读/etc/init.d/httpd,注意到所有的字符串都像$"Hello World."这样被引用。我从来没有见过这种语法,也在谷歌上搜不到任何相关信息。

摘录:

if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
    RETVAL=6
    echo $"not reloading due to configuration syntax error"
    failure $"not reloading $httpd due to configuration syntax error"

这是怎么回事?


@tripleee 三年后将其标记为重复? - Sammitch
将答案集中在一个地方而不是在整个网站上散布重复内容是一项长期维护的过程。理想情况下,有人应该早些时候就注意到了重复,但这并不意味着现在标记它没有用处。 - tripleee
1个回答

6

From man 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
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double 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)
          \uHHHH the Unicode (ISO/IEC 10646) character whose value is  the
                 hexadecimal value HHHH (one to four hex digits)
          \UHHHHHHHH
                 the  Unicode (ISO/IEC 10646) character whose value is the
                 hexadecimal value HHHHHHHH (one to eight hex digits)
          \cx    a control-x character

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

   A double-quoted string preceded by a dollar sign ($"string") 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.

这意味着字符串可以根据使用的语言环境进行翻译吗?(如果有可用的翻译) - jimm-cl
谢谢!通过其他渠道查找,也发现了一些有关如何实现它的文档:http://tldp.org/LDP/abs/html/localization.html - Sammitch
这里有一篇关于Bash本地化和echo命令的文章:http://www.cyberciti.biz/faq/bash-localization-with-echo-command/ - Jakub Kotowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接