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

sh sh04.sh 和 ./sh04.sh 中 echo 的结果不一样

  •  
  •   Tiande ·
    Tiande · 2015-05-25 21:03:51 +08:00 · 2611 次点击
    这是一个创建于 3257 天前的主题,其中的信息可能已经有所发展或是发生改变。

    script 中的 echo 语句是这样的:

    echo -e "You SHOULD input 2 numbers, I will cross them!\n"
    

    sh sh04.sh 执行的时候(非预期结果):

    tiande@tiande-debian:~/Practice$ sh sh04.sh
    -e You SHOULD input 2 numbers, I will cross them! 
                                    #这是 \n 的空行
    

    ./sh04.sh(或 source sh04.sh)执行的时候(理想结果):

    tiande@tiande-debian:~/Practice$ ./sh04.sh
    You SHOULD input 2 numbers, I will cross them! 
                                    #这是 \n 的空行
    

    然而 bash 并没有什么特殊处理

    echo 也未作改变:

    tiande@tiande-debian:~/Practice$ type echo
    echo 是 shell 内建
    

    把 script 里的 PATH 写到当前 bash 也不行。

    测试 script :

    #!/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "You SHOULD input 2 numbers, I will cross them! \n"
    

    QUESTION

    我只知道 ./sh04.sh (OR sourcesh 04.sh) 是在当前 bash 执行
    sh sh04.sh 是 sleep 当前 bash , 启用新 session
    但是并没有发现 环境变量 有特别的变化
    拉么是拉里粗戳了

    求一语道破人生迷思
    谢谢 ;)

    第 1 条附言  ·  2015-05-25 21:35:34 +08:00
    已解决啦
    debian 8.0 sid 的 /bin/sh 指向的是 dash 不是 bash
    大家玩得愉快
    ;)
    12 条回复    2015-05-25 21:36:32 +08:00
    msg7086
        1
    msg7086  
       2015-05-25 21:08:11 +08:00   ❤️ 1
    sh sh04.sh 显然是调用了sh。
    如果你需要调用bash,一种是用 ./sh04.sh 这样会检查第一行并调用bash。
    又或者运行 bash sh04.sh
    Tiande
        2
    Tiande  
    OP
       2015-05-25 21:12:34 +08:00
    @msg7086 但是 echo 的 -e 参数怎么莫名其妙的变成 string 显示了
    而且 -e 显示后,\n 竟然还能换行。
    binux
        3
    binux  
       2015-05-25 21:16:38 +08:00   ❤️ 1
    bash 的 echo 和 sh 的 echo 并不相同
    Tiande
        4
    Tiande  
    OP
       2015-05-25 21:19:37 +08:00
    @binux 该怎么知道哪里不同。
    我在里面加上

    type echo
    man echo | grep '\-e'

    之后

    sh sh04.sh

    得到的结果和当前 bash 的一样。
    binux
        5
    binux  
       2015-05-25 21:27:17 +08:00   ❤️ 1
    @dtdnqsb 运行 sh 进去试试不就知道了
    msg7086
        6
    msg7086  
       2015-05-25 21:28:37 +08:00   ❤️ 1
    choury
        7
    choury  
       2015-05-25 21:29:59 +08:00   ❤️ 1
    一般echo命令shell都会内置,默认不会调用PATH里面的
    matthewgao
        8
    matthewgao  
       2015-05-25 21:32:10 +08:00   ❤️ 1
    sh 的 echo -e 支持是有问题的, 我遇到过
    Tiande
        9
    Tiande  
    OP
       2015-05-25 21:32:59 +08:00
    @binux 谢谢 日了狗了
    /bin/sh 指向 dash 不是 bash ...
    么么哒,继续愉快的打代码。
    Tiande
        10
    Tiande  
    OP
       2015-05-25 21:35:20 +08:00
    @msg7086
    @choury
    @matthewgao
    已解决啦
    debian 8.0 sid 的 /bin/sh 指向的是 dash 不是 bash
    msg7086
        11
    msg7086  
       2015-05-25 21:35:25 +08:00   ❤️ 1
    @dtdnqsb dash要尽可能支持sh本体的规范,所以很多扩展跟bash不太一样的。
    Tiande
        12
    Tiande  
    OP
       2015-05-25 21:36:32 +08:00
    @msg7086
    好的,先留个印象,以后闲了再研究它。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3304 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.