Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Tiande
V2EX  ›  Linux

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

  •  
  •   Tiande · May 25, 2015 · 3252 views
    This topic created in 4081 days ago, the information mentioned may be changed or developed.

    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
    但是并没有发现 环境变量 有特别的变化
    拉么是拉里粗戳了

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

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

    type echo
    man echo | grep '\-e'

    之后

    sh sh04.sh

    得到的结果和当前 bash 的一样。
    binux
        5
    binux  
       May 25, 2015   ❤️ 1
    @dtdnqsb 运行 sh 进去试试不就知道了
    choury
        7
    choury  
       May 25, 2015   ❤️ 1
    一般echo命令shell都会内置,默认不会调用PATH里面的
    matthewgao
        8
    matthewgao  
       May 25, 2015   ❤️ 1
    sh 的 echo -e 支持是有问题的, 我遇到过
    Tiande
        9
    Tiande  
    OP
       May 25, 2015
    @binux 谢谢 日了狗了
    /bin/sh 指向 dash 不是 bash ...
    么么哒,继续愉快的打代码。
    Tiande
        10
    Tiande  
    OP
       May 25, 2015
    @msg7086
    @choury
    @matthewgao
    已解决啦
    debian 8.0 sid 的 /bin/sh 指向的是 dash 不是 bash
    msg7086
        11
    msg7086  
       May 25, 2015   ❤️ 1
    @dtdnqsb dash要尽可能支持sh本体的规范,所以很多扩展跟bash不太一样的。
    Tiande
        12
    Tiande  
    OP
       May 25, 2015
    @msg7086
    好的,先留个印象,以后闲了再研究它。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3382 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:39 · PVG 19:39 · LAX 04:39 · JFK 07:39
    ♥ Do have faith in what you're doing.