Reputation: 43
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
Upvotes: 4
Views: 1772
Reputation: 20688
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. Theprintf
utility can be used portably to emulate any of the traditional behaviors of theecho
utility [...]
I have this _echo()
in my bashrc:
#
# _echo [-n] args...
#
function _echo()
{
local eol='\n'
if [[ $1 == '-n' ]]; then
eol=''
shift
fi
if [[ $1 == '--' ]]; then
shift
fi
printf "%s$eol" "$*"
}
Upvotes: 4