Bash中嵌套单引号

Bash中嵌套单引号

解决

> echo $'\'single quote phrase\' "double quote phrase"'
'single quote phrase' "double quote phrase"

帮助文档

单引号将剥夺其中的所有字符的特殊含义,而双引号中的'$'(参数替换)和'`'(命令替换)是例外

From man bash

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

单引号不会出现在两个单引号之间,即使加了\

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:

单引号的字符串使用$中的\会被替换成ANSI C标准

bash \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) \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.

示例

# 没有被转义
> echo '\n'
\n
# 转义了换行
> echo $'\n'

> echo "\'"
\'
> echo $"\'"
\'
> echo $'\''
'

本文作者:朝圣

本文链接:www.zh-noone.cn/2020/7/shell单引号

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0许可协议。转载请注明出处!

python数组切片
0 条评论