linux - "echo"sh 和 bash 输出不同的答案

标签 linux bash shell echo dash-shell

我用三种方式运行我的脚本,但输出不同,你能解释一下为什么会这样吗?谢谢!! 这是我的脚本

#!/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!

还有一些其他信息

  1. 操作系统:Ubuntu 16.04.3
  2. 输入 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. The printf utility can be used portably to emulate any of the traditional behaviors of the echo utility [...]

关于linux - "echo"sh 和 bash 输出不同的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996612/

相关文章:

c++ bash 添加到 PATH

bash - 针对 BASH 脚本中的 -n 选项进行测试始终返回 true

shell - 我可以在某个地方使用 ssh,运行一些命令,然后给自己一个提示吗?

linux - 如何在 bash 中的某一行之后对文件进行排序?

php - Shell_exec 使用相同的输入给出不同的输出

linux - 在新的或现有的 VT 中使用自定义 shell 登录

linux - 创建 Bash 脚本来检查和禁用 SELinux : How to Prompt User Input?

linux - CLion 不完整的消息生成

linux - 编译linux驱动的步骤

bash - 在 bash PS1 中设置变量