问题详情
echo \’是’ echo “\'”是\’ echo $’\”是’ 这是什么原因
回答
From man bash – Quoting section:
- Escape Character: A non-quoted backslash (
\) is the escape character. It preserves the literal value of the next character. - Double Quotes: The backslash retains its special meaning only when followed by
$,`,",\, or newline. Within double quotes, a backslash that is not followed by one of these characters remains literal. - ANSI-C Quoting: Words of the form
$'string'are treated specially. The string is interpreted as if specified in ANSI C, so\'becomes a single quote.
在 未加引号的单词(unquoted word) 中,反斜杠 \ 可以转义 任何字符,包括 '、"、字母、数字等,在无引号时,\ 总是去除下一个字符的特殊含义(即使它本来没特殊含义)。
第二条:echo "\'"→ 输出\'
原因:在双引号中,\'不会被转义!
在双引号 "..." 中,反斜杠 \ 只对以下字符有转义作用:
$, `, ", \, newline(换行)
🔍 第三条:echo $'\''→ 输出'
原因:$'...'是 ANSI-C 引用,支持 C 风格转义
在 $'...' 中:
-
\'表示字面单引号(C 语言规则) - 所以
$'\''= 一个'字符
✅ 输出:'
Bash 特性:$'...' 中支持 C 风格转义序列,例如:
-
\n→ 换行 -
\t→ 制表符 -
\'→ 字面单引号 -
\"→ 字面双引号 -
\\→ 字面反斜杠
\n → 换行只有echo -e和$”这种黑魔法识别,其他都不会将其识别换行符,因为它不是真正的换行符,包括在ssh “”里面,
版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.stntk.com/question/240.html
还没有评论呢,快来抢沙发~