码迷,mamicode.com
首页 > 系统相关 > 详细

shell中$后加引号有什么用($"string"和$'string')

时间:2018-02-19 21:11:01      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:str   作者   hhhh   seq   ack   cte   man   本地   current   

有些时候在某些服务管理脚本中看到$"$string",经过一些测试,发现引号外面的$有和没有是一样的。刚才翻了下man bash,找到了解释。

(1).如果没有特殊定制bash环境或有特殊需求,$"string"和"string"是完全等价的,使用$""只是为了保证本地化。

以下是man bash关于$""的解释:

       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.

(2).还有$后接单引号的$‘string‘,这在bash中被特殊对待:会将某些反斜线序列(如\n,\t,\",\‘等)继续转义,而不认为它是字面符号(如果没有$符号,单引号会强制将string翻译为字面符号,包括反斜线)。简单的例子:

[[email protected] ~]# echo a\nb
a\nb
[[email protected] ~]# echo $a\nb
a
b

以下是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

 

回到Linux系列文章大纲:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
回到网站架构系列文章大纲:http://www.cnblogs.com/f-ck-need-u/p/7576137.html
回到数据库系列文章大纲:http://www.cnblogs.com/f-ck-need-u/p/7586194.html
转载请注明出处:http://www.cnblogs.com/f-ck-need-u/p/8451982.html

注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!

shell中$后加引号有什么用($"string"和$'string')

标签:str   作者   hhhh   seq   ack   cte   man   本地   current   

原文地址:https://www.cnblogs.com/f-ck-need-u/p/8454364.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!