“回声”通过sh和bash输出不同的答案 [英] "echo" output different answer by sh and bash
问题描述
我使用三种方式运行了脚本,但输出却不同,您能向我解释一下为什么这样工作吗?谢谢!!
这是我的脚本
I ran my script using three ways and the output was different, could you explain to me why it works like that? Thanks!! Here is my script
#!/bin/bash
#Program:
# This program shows "Hello World!" in your screen.
echo -e "Hello World! \a\n"
exit 0
如果我通过bash和./来运行它,如 bash sh01.sh ,则输出为
And if i run it by bash and ./ like bash sh01.sh the output is
世界,您好!
Hello World!
但是,如果我使用 sh sh01.sh 之类的sh会是
However, if i use sh like sh sh01.sh it would be like
-e Hello World!
-e Hello World!
还有其他信息
- 操作系统:Ubuntu 16.04.3
- type sh->破折号
推荐答案
echo
不太便于携带(甚至Bash的 echo
在不同的OS上的行为也可能不同,这些OS在编译Bash时可能使用不同的默认选项。您可以使用 printf
。根据 posix :
echo
is not very portable (even Bash's echo
may behave differently on different OSes which may use different default options when compiling Bash). You can use printf
. According to posix:
除非在两个POSIX系统中都使用
echo
,否则不可能在所有POSIX系统上移植使用echo
c $ c>(作为第一个参数)和转义序列被省略。
printf
实用程序可用于模拟echo
实用程序的任何传统行为[。 ..]
It is not possible to use
echo
portably across all POSIX systems unless both-n
(as the first argument) and escape sequences are omitted. Theprintf
utility can be used portably to emulate any of the traditional behaviors of theecho
utility [...]
这篇关于“回声”通过sh和bash输出不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!