bash - 字符串封装如 $"Hello World."

标签 bash

<分区>

我刚刚在 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"

这是怎么回事?

最佳答案

来自 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.

关于bash - 字符串封装如 $"Hello World.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22414932/

相关文章:

linux - Bash:将当前路径剪切到某个文件夹

bash - 重定向到使用/dev/tty 的脚本

linux - ls -l 在 2 个服务器中输出不同

json - 使用 bash 将文件内容转换为单个 JSON 字符串值

c - 将输入发送到 C 程序并打印它们

linux - cp 命令不在 sh\bash 脚本中创建目录,为什么?

linux - 使用 `date` 命令获取上个月、当前月和下个月

linux - 在两列中显示重复行,每列三列

bash - noop [ :] in bash? 的用例是什么

bash - 我们如何根据 unix 中的给定日期获取工作日