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

2015-05-25 21:03:51 +08:00
 Tiande

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

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

2649 次点击
所在节点    Linux
12 条回复
msg7086
2015-05-25 21:08:11 +08:00
sh sh04.sh 显然是调用了sh。
如果你需要调用bash,一种是用 ./sh04.sh 这样会检查第一行并调用bash。
又或者运行 bash sh04.sh
Tiande
2015-05-25 21:12:34 +08:00
@msg7086 但是 echo 的 -e 参数怎么莫名其妙的变成 string 显示了
而且 -e 显示后,\n 竟然还能换行。
binux
2015-05-25 21:16:38 +08:00
bash 的 echo 和 sh 的 echo 并不相同
Tiande
2015-05-25 21:19:37 +08:00
@binux 该怎么知道哪里不同。
我在里面加上

type echo
man echo | grep '\-e'

之后

sh sh04.sh

得到的结果和当前 bash 的一样。
binux
2015-05-25 21:27:17 +08:00
@dtdnqsb 运行 sh 进去试试不就知道了
msg7086
2015-05-25 21:28:37 +08:00
choury
2015-05-25 21:29:59 +08:00
一般echo命令shell都会内置,默认不会调用PATH里面的
matthewgao
2015-05-25 21:32:10 +08:00
sh 的 echo -e 支持是有问题的, 我遇到过
Tiande
2015-05-25 21:32:59 +08:00
@binux 谢谢 日了狗了
/bin/sh 指向 dash 不是 bash ...
么么哒,继续愉快的打代码。
Tiande
2015-05-25 21:35:20 +08:00
@msg7086
@choury
@matthewgao
已解决啦
debian 8.0 sid 的 /bin/sh 指向的是 dash 不是 bash
msg7086
2015-05-25 21:35:25 +08:00
@dtdnqsb dash要尽可能支持sh本体的规范,所以很多扩展跟bash不太一样的。
Tiande
2015-05-25 21:36:32 +08:00
@msg7086
好的,先留个印象,以后闲了再研究它。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/193661

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX