- 论坛徽章:
- 60
|
本帖最后由 reyleon 于 2016-07-08 15:19 编辑
先记住 echo 与 echo -e 是有区别的, 后面再说, 先说shell
首先要了解bash shell中相关龟腚:
1. bash shell中龟腚, 没有被引用的反斜杠(\)是转义字符, 所以没有被引用的 \n 实际上是反斜杠\后面的字符n的字面意思. 就是n.
如 \', \`,\\, \$, 都是输出反斜杠后面的那个字符- $ echo \`
- `
- $ echo \'
- '
- $ echo \\
- \
- $ echo \$
- $
- $ echo \n
- n
复制代码 2. 在双引用中, 反斜杠就没那么屌了, 只有当反斜杠后面的字符是: $, `, ", \ 这四个字符时, 反斜杠才有特异功能,如:- $ echo "\4"
- \4
- $ echo "\c"
- \c
- $ echo "\$" #反斜杠后面为美元符号,反斜杠就起到了转义的作用
- $
- $ echo "\"" #同上,反斜杠后面为双引号,反斜杠有转义作用
- "
- $ echo "\`" #反斜杠后面为反引号,反斜杠有转义作用
- `
- $ echo "\\" #反斜杠后面是反斜杠,反斜杠有转义作用
- \
- $ echo "\%"
- \%
复制代码 3. 在单引号中, 反斜杠就完全掀不起浪了, 它就是反斜杠:- $ echo '\"'
- \"
- $ echo '\`'
- \`
复制代码 在shell中输入的命令, 首先是被shell解析, 然后再是被命令解析.
首先, echo 的-e选项, 就是解析反斜杠的作用:- -e enable interpretation of backslash escapes
复制代码- $ DATE=$(date)
- $ echo -e Today is $DATE\n\n # shell解析, \n\n因为反斜杠转义, \n\n就变成了nn. echo命令后面压根就没有反斜杠了.所以与不加-e的结果是一样的.
- Today is Fri Jul 8 15:02:03 CST 2016nn
- $ echo Today is $DATE\n\n
- Today is Fri Jul 8 15:02:03 CST 2016nn
复制代码 再看, Today is $DATE\n\n 被引用之后, "Today is $DATE\n\n", \n中的反斜杠在shell的解析中就失去了转义的作用, \n\n还是\n\n. 然后再传递个echo -e
echo -e 会对反斜杠进行解析, \n 被echo -e 解析为了换行.- $ echo "Today is $DATE\n\n" # shell 不对反斜杠做转义解释, 因echo 没有 -e 选项, 所以原样输出
- Today is Fri Jul 8 15:02:03 CST 2016\n\n
- $ echo -e "Today is $DATE\n\n" # 加了 -e , echo 对 \n解析, 换行.
- Today is Fri Jul 8 15:02:03 CST 2016
复制代码 更多详细内容, man bash :- QUOTING
- Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special
- treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.
- Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent
- itself.
- When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually
- !, must be quoted to prevent history expansion.
- There are three quoting mechanisms: the escape character, single quotes, and double quotes.
- A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the
- exception of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line
- continuation (that is, it is removed from the input stream and effectively ignored).
- 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.
- Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \,
- and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes. The backslash
- retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be
- quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing
- in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.
- The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).
- Words of the form string' are treated specially. The word expands to string, with backslash-escaped characters replaced as speci-
- fied 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)
- \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.
复制代码 中文说明:
卧槽, 我发现我好无聊...
|
评分
-
查看全部评分
|