“echo” output different answer by sh and bash

回眸只為那壹抹淺笑 提交于 2019-12-11 07:59:12

问题


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

And if i run it by bash and ./ like bash sh01.sh the output is

Hello World!

However, if i use sh like sh sh01.sh it would be like

-e Hello World!

And Here is some other information

  1. OS: Ubuntu 16.04.3
  2. type sh -> dash

回答1:


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:

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 [...]



来源:https://stackoverflow.com/questions/46996612/echo-output-different-answer-by-sh-and-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!