Reputation: 1135
I got different behaviors with the same command echo -ne "hello\n"
with bash and with dash. See below :
$ bash -c 'echo -ne "hello\n"'
hello
$ dash -c 'echo -ne "hello\n"'
-ne hello
Why is that ? I don't understand at all…
My system :
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
Upvotes: 7
Views: 3813
Reputation: 13
It's been stated in a previous answer that it's because dash doesn't use/understand the -n argument for echo, but this is not true.
Dash uses the -n argument to not print out a newline. But the example uses -ne. The following are all from dash version 0.5.12-2 in Debian:
$ echo -n string
string$
$ echo -n -e string
-e string$
$ echo -ne string
-ne string
$
As you can see:
In "echo -n string", it prints string with no newline.
In "echo -n -e string", it prints no newline because -n is an allowed argument that dash recognizes, and then it treats -e as a string because it's not a valid argument according to POSIX.
In "echo -ne string", it treats -ne as a string because it's not a valid argument according to POSIX (because of the "e" part).
Upvotes: 0
Reputation: 2806
While echo
implementation in bash
is not POSIX and Unix conformed by default, you can alter its behavior at run time or compile time.
At run time, with xpg_echo
and in POSIX mode, bash
echo
became conformant:
$ env BASHOPTS=xpg_echo SHELLOPTS=posix bash -c 'echo -ne "hello\n"'
-ne hello
or:
$ env BASHOPTS=xpg_echo POSIXLY_CORRECT= bash -c 'echo -ne "hello\n"'
-ne hello
At compile time, you can pass --enable-xpg-echo-default
and --enable-strict-posix-default
options to configure
script.
Upvotes: 2
Reputation: 80931
The POSIX specification for echo
doesn't support any arguments. See it here.
And while the spec mentions -n
it does so to say that it is not an option and is either an implementation defined case or to be treated as a string.
So dash
is doing exactly that.
bash
, on the other hand, has non-conforming behavior in a number of ways.
This is why the use of echo
is generally discouraged in favor of the using printf
which has a much better specification and much better portable behavior.
Upvotes: 11