shell中$后加引号有什么用($"string"和$'string')
2018-02-19 20:32
260 查看
有些时候在某些服务管理脚本中看到$"$string"或$"string",经过一些测试,又发现引号外面的$有和没有是一样的。一直也没去找究竟,刚才有人问了我,于是就去翻了下man bash,找到了解释。
(1).如果没有特殊定制bash环境或有特殊需求,$"string"和"string"是完全等价的,使用$""只是为了保证本地化。
以下是man bash关于$""的解释:
(2).还有$后接单引号的$'string',这在bash中被特殊对待:会将某些反斜线序列(如\n,\t,\",\'等)继续转义,而不认为它是字面符号(如果没有$符号,单引号会强制将string翻译为字面符号,包括反斜线)。简单的例子:
以下是man bash里关于$'的说明:
回到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
注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!
(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翻译为字面符号,包括反斜线)。简单的例子:
[root@xuexi ~]# echo 'a\nb' a\nb [root@xuexi ~]# 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中的"2>&1"是什么意思?
- shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符`
- String str=null; 和String str=""有什么区别
- String s=new String("xyz");创建了几个String Object?二者之前的区别是什么?
- mybatis中SQL语句单双引号的问题java.lang.NumberFormatException: For input string: "Y"
- Shell十三问之四:""(双引号)与' '(单引号)差在哪
- shell中的"2>&1"是什么意思?
- String s;和String s=null;和String s="a";有什么区别?
- shell脚本 -d 是目录文件,那么-e,-f分别是什么?还有"! -e"这又是什么意思呢?
- string变量字符串的结束标志是什么,我看书上好像和c不一样,c的char *是以"\0"结束但c++好像不是
- 对于String对象,可以使用"="赋值,也可以使用"new"关键字赋值,两种方式有什么区别?
- string path = @"c:\temp\MyTest.txt";这里的@是什么意思
- String x = "abc" 和 String x = new String("abc")有什么区别?
- String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思
- -----String s = new String("xyz");创建了几个String Object? 二者之间有什么区别?
- Page.ClientScript.RegisterStartupScript(this.GetType(), "","")引号里面具体代表什么?
- String str=new String("a")和String str = "a"有什么区别?
- Java (JSONArray、JSONObject、String字符串中)引号""替换成"-"
- shell十三问-4) " "(双引号) 与 ' '(单引号)差在哪
- shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符`