V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
silenceeeee
V2EX  ›  Linux

shell here document 问题

  •  
  •   silenceeeee · 2016-11-15 09:58:07 +08:00 · 1668 次点击
    这是一个创建于 2718 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下代码:

    1 #! /bin/bash

    2 v1=$(cat <<End-of-message

    3 -------------------------------------

    4 This is line 1 of the message. |

    5 This is line 2 of the message. |

    6 This is line 3 of the message. |

    7 This is line 4 of the message. |

    8 This is the last line of the message.

    9 -------------------------------------

    10 End-of-message

    11 )

    12 echo "${v1}"

    13 v2="line1\nline2"

    14 echo "${v2}"

    输出:


    This is line 1 of the message. |

    This is line 2 of the message. |

    This is line 3 of the message. |

    This is line 4 of the message. |

    This is the last line of the message.


    line1\nline2

    疑问:为什么 echo "${v1}"输出内容时会输出换行,而 echo "${v2}"却不会输出换行(加上-e 参数可以正常输出换行)

    2 条回复    2016-11-15 10:21:07 +08:00
    lululau
        1
    lululau  
       2016-11-15 10:13:46 +08:00
    因为 v1 里面有换行符,所以就输出换行了; v2 里没有换行符,所以就不会输出换行

    echo -e 可以把一些转义符转成对应的实际字符
    silenceeeee
        2
    silenceeeee  
    OP
       2016-11-15 10:21:07 +08:00
    @lululau 好吧.貌似确实就是这么简单~_~ 一下没理解过来 谢谢了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3738 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:34 · PVG 18:34 · LAX 03:34 · JFK 06:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.