免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1665 | 回复: 4
打印 上一主题 下一主题

[文本处理] 为什么echo命令的控制字符只在“”中生效? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-07-08 14:09 |只看该作者 |倒序浏览
我是一个新人小白,最近开始学习shell的,今天遇到一个问题实在是想不通了。问题如下:
#!/bin/sh
DATE=$(date)
echo -e Today is $DATE\n\n

echo -e "Today is $DATE\n\n"

echo -e "Today is $DATE"\n\n

执行结果:

marmot@sles11xen:~> sh t1.sh
Today is Fri Jul 8 14:00:47 CST 2016nn
Today is Fri Jul  8 14:00:47 CST 2016


Today is Fri Jul  8 14:00:47 CST 2016nn

在echo命令中 双引号“”到底是一个怎样的存在? 为什么反斜线控制符,必须放在双引号中,才生效?   我看了一点十三问的帖子,\这种meta在双引号中不是应该被关闭的嘛?

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
2 [报告]
发表于 2016-07-08 15:17 |只看该作者
本帖最后由 reyleon 于 2016-07-08 15:19 编辑

先记住 echo 与 echo -e 是有区别的, 后面再说, 先说shell

首先要了解bash shell中相关龟腚:

1. bash shell中龟腚, 没有被引用的反斜杠(\)是转义字符, 所以没有被引用的 \n 实际上是反斜杠\后面的字符n的字面意思. 就是n.
如 \', \`,\\, \$, 都是输出反斜杠后面的那个字符
  1. $ echo \`
  2. `
  3. $ echo \'
  4. '
  5. $ echo \\
  6. \
  7. $ echo \$
  8. $
  9. $ echo \n
  10. n
复制代码
2.  在双引用中, 反斜杠就没那么屌了, 只有当反斜杠后面的字符是: $, `, ", \ 这四个字符时, 反斜杠才有特异功能,如:
  1. $ echo "\4"
  2. \4
  3. $ echo "\c"
  4. \c
  5. $ echo "\$" #反斜杠后面为美元符号,反斜杠就起到了转义的作用
  6. $
  7. $ echo "\"" #同上,反斜杠后面为双引号,反斜杠有转义作用
  8. "
  9. $ echo "\`" #反斜杠后面为反引号,反斜杠有转义作用
  10. `
  11. $ echo "\\" #反斜杠后面是反斜杠,反斜杠有转义作用
  12. \
  13. $ echo "\%"
  14. \%
复制代码
3. 在单引号中, 反斜杠就完全掀不起浪了, 它就是反斜杠:
  1. $ echo '\"'
  2. \"
  3. $ echo '\`'
  4. \`
复制代码
在shell中输入的命令, 首先是被shell解析, 然后再是被命令解析.
首先, echo 的-e选项, 就是解析反斜杠的作用:
  1. -e     enable interpretation of backslash escapes
复制代码
  1. $ DATE=$(date)
  2. $ echo -e Today is $DATE\n\n # shell解析, \n\n因为反斜杠转义, \n\n就变成了nn. echo命令后面压根就没有反斜杠了.所以与不加-e的结果是一样的.
  3. Today is Fri Jul 8 15:02:03 CST 2016nn

  4. $ echo Today is $DATE\n\n
  5. 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 解析为了换行.
  1. $ echo "Today is $DATE\n\n"   # shell 不对反斜杠做转义解释, 因echo 没有 -e 选项, 所以原样输出
  2. Today is Fri Jul  8 15:02:03 CST 2016\n\n

  3. $ echo -e "Today is $DATE\n\n" # 加了 -e , echo 对 \n解析, 换行.
  4. Today is Fri Jul  8 15:02:03 CST 2016


复制代码
更多详细内容, man bash :
  1. QUOTING
  2.        Quoting  is  used  to  remove the special meaning of certain characters or words to the shell.  Quoting can be used to disable special
  3.        treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

  4.        Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it  is  to  represent
  5.        itself.

  6.        When  the  command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually
  7.        !, must be quoted to prevent history expansion.

  8.        There are three quoting mechanisms: the escape character, single quotes, and double quotes.

  9.        A non-quoted backslash (\) is the escape character.  It preserves the literal value of the  next  character  that  follows,  with  the
  10.        exception  of  <newline>.   If  a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line
  11.        continuation (that is, it is removed from the input stream and effectively ignored).

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

  14.        Enclosing  characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \,
  15.        and, when history expansion is enabled, !.  The characters $ and ` retain their special meaning within double quotes.   The  backslash
  16.        retains  its  special  meaning only when followed by one of the following characters: $, `, ", \, or <newline>.  A double quote may be
  17.        quoted within double quotes by preceding it with a backslash.  If enabled, history expansion will be performed unless an !   appearing
  18.        in double quotes is escaped using a backslash.  The backslash preceding the !  is not removed.

  19.        The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).

  20.        Words  of  the form string' are treated specially.  The word expands to string, with backslash-escaped characters replaced as speci-
  21.        fied by the ANSI C standard.  Backslash escape sequences, if present, are decoded as follows:
  22.               \a     alert (bell)
  23.               \b     backspace
  24.               \e
  25.               \E     an escape character
  26.               \f     form feed
  27.               \n     new line
  28.               \r     carriage return
  29.               \t     horizontal tab
  30.               \v     vertical tab
  31.               \\     backslash
  32.               \'     single quote
  33.               \"     double quote
  34.               \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
  35.               \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
  36.               \cx    a control-x character

  37.        The expanded result is single-quoted, as if the dollar sign had not been present.

  38.        A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the  current  locale.
  39.        If the current locale is C or POSIX, the dollar sign is ignored.  If the string is translated and replaced, the replacement is double-
  40.        quoted.
复制代码
中文说明:



卧槽, 我发现我好无聊...



评分

参与人数 3信誉积分 +21 收起 理由
Herowinter + 10 很给力!
ll104567 + 5 淡定
sync_1521 + 6 6

查看全部评分

论坛徽章:
10
15-16赛季CBA联赛之同曦
日期:2016-06-11 19:22:4115-16赛季CBA联赛之深圳
日期:2020-05-31 16:13:5615-16赛季CBA联赛之同曦
日期:2020-01-28 12:42:47每日论坛发贴之星
日期:2016-08-09 06:20:00程序设计版块每日发帖之星
日期:2016-08-09 06:20:00每日论坛发贴之星
日期:2016-07-12 06:20:00程序设计版块每日发帖之星
日期:2016-07-12 06:20:00程序设计版块每日发帖之星
日期:2016-07-06 06:20:00程序设计版块每日发帖之星
日期:2016-07-04 06:20:0015-16赛季CBA联赛之佛山
日期:2021-02-26 09:33:41
3 [报告]
发表于 2016-07-09 09:45 |只看该作者
在单引号中也是生效的@-@
~/pwb/tmp/sh/xie# echo -e 'asd\n'
asd

论坛徽章:
0
4 [报告]
发表于 2016-07-09 18:08 |只看该作者
感谢大神,完全明白了,讲的真透彻真详细。。。谢谢谢谢。回复 2# reyleon


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
5 [报告]
发表于 2016-07-09 23:09 |只看该作者
$ echo -e abc\\nefg
abc
efg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP