我用三种方式运行我的脚本,但输出不同,你能解释一下为什么会这样吗?谢谢!! 这是我的脚本
#!/bin/bash
#Program:
# This program shows "Hello World!" in your screen.
echo -e "Hello World! \a\n"
exit 0
如果我通过 bash 和 ./运行它,就像 bash sh01.sh 输出是
Hello World!
但是,如果我像 sh sh01.sh 这样使用 sh,它会像
-e Hello World!
还有一些其他信息
- 操作系统:Ubuntu 16.04.3
- 输入 sh -> 破折号
最佳答案
echo
不是很便携(即使是 Bash 的 echo
在编译 Bash 时可能使用不同默认选项的不同操作系统上也可能表现不同)。您可以使用 printf
。根据posix :
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 [...]
关于linux - "echo"sh 和 bash 输出不同的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996612/