Skip to contentSkip to conversation

echo -n does not work as described

bug? Kevin Korb 6 participants 13 messages Nov 12, 2015 – Nov 19, 2015

lore
Share & tools
lore

Kevin Korb

author
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:
echo [-n] args...
Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a newline is output following the arguments.

However, in actual usage:
% dash

$ echo testing
testing
$ echo -n testing
- -n testing
$ /bin/echo -n testing
testing$

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
- -*~
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
- -*~
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZD8bIACgkQVKC1jlbQAQc0DQCaAt6x4XveLhPjaPiPaSG8ll5a 3ZEAn0J5mkr1wd7Nd2l3Q6xKpB2LZ39+
=lljb
-----END PGP SIGNATURE-----

#1 lore raw
Quoted 3 lines · Kevin Korb

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:

Which man page? Coreutils'?

Read it further:

Quoted 3 lines

NOTE: your shell may have its own version of echo, which usually super‐
sedes the version described here. Please refer to your shell's docu‐ mentation for details about the options it supports.

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

A string to be written to standard output. If the first operand is
-n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

Quoted 2 lines
$ echo -n testing
-n testing

Dash's implementation-defined behavior for -n is to treat it the same as every other string. Thus, this is not a bug in dash, at least according to POSIX.

Quoted 2 lines

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

It's probably not the only bashism you've been relying on. You may want to try using checkbashisms
(http://sourceforge.net/projects/checkbaskisms/) to help you find other portability pitfalls.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 7 lines · Eric Blake
Quoted 4 lines · Kevin Korb

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:

Which man page? Coreutils'?

I was quoting the dash man page. I did quote the result of the coreutils version of echo (/bin/echo) which worked correctly.

Quoted 9 lines

Read it further:

Quoted 4 lines

NOTE: your shell may have its own version of echo, which usually super‐ sedes the version described here. Please refer to your shell's docu‐ mentation for details about the options it supports.

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

This isn't my problem. I am trying to use dash as a replacement for /bin/sh and it is failing horribly. I am not the authors of the problem scripts. Plus, frankly, it is absurd to require printf where echo [-n] should work correctly according to the dash documentation.

Quoted 11 lines

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

A string to be written to standard output. If the first operand
is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

Quoted 1 line
$ echo -n testing -n testing

Dash's implementation-defined behavior for -n is to treat it the same as every other string. Thus, this is not a bug in dash, at least according to POSIX.

Either this is a bug in dash or in dash's man page. The man page is clear about how echo and echo -n should work. But the reality is very different.

Quoted 2 lines
Quoted 2 lines

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

This isn't stuff I have written. This is stuff that I am tired of modifying to be dash compatible.

Either dash's echo command should operate as documented or the documentation should be corrected.

Quoted 5 lines

It's probably not the only bashism you've been relying on. You may want to try using checkbashisms
(http://sourceforge.net/projects/checkbaskisms/) to help you find other portability pitfalls.

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZECh4ACgkQVKC1jlbQAQfK3ACeN1EC20BVyzwnSXTndBCchYS0 tHkAoKANsc2eL1imSW6u8oSpJ9VaerOc
=20HJ
-----END PGP SIGNATURE-----

author
#3 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 8 lines · Kevin Korb
Quoted 7 lines · Eric Blake
Quoted 4 lines · Kevin Korb

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:

Which man page? Coreutils'?

no, /usr/share/man/man1/dash.1.xz on my box

Quoted 12 lines

I was quoting the dash man page. I did quote the result of the coreutils version of echo (/bin/echo) which worked correctly.

Quoted 9 lines

Read it further:

Quoted 4 lines

NOTE: your shell may have its own version of echo, which usually super‐ sedes the version described here. Please refer to your shell's docu‐ mentation for details about the options it supports.

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

If dash refuses to supply a compatible version of echo then dash must not include a builtin version of echo. If dash didn't have a broken embedded version of echo I wouldn't have a problem.

Quoted 17 lines

This isn't my problem. I am trying to use dash as a replacement for /bin/sh and it is failing horribly. I am not the authors of the problem scripts. Plus, frankly, it is absurd to require printf where echo [-n] should work correctly according to the dash documentation.

Quoted 11 lines

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

A string to be written to standard output. If the first operand
is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

Quoted 1 line
$ echo -n testing -n testing

Dash's implementation-defined behavior for -n is to treat it the same as every other string. Thus, this is not a bug in dash, at least according to POSIX.

Then why does the man page say otherwise?

Quoted 13 lines

Either this is a bug in dash or in dash's man page. The man page is clear about how echo and echo -n should work. But the reality is very different.

Quoted 2 lines
Quoted 2 lines

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

This isn't stuff I have written. This is stuff that I am tired of modifying to be dash compatible.

Either dash's echo command should operate as documented or the documentation should be corrected.

Quoted 9 lines
Quoted 4 lines

It's probably not the only bashism you've been relying on. You may want to try using checkbashisms
(http://sourceforge.net/projects/checkbaskisms/) to help you find other portability pitfalls.

-- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZEC3oACgkQVKC1jlbQAQfJawCeIdQetXk4zloxdAWk0+ZmreeQ bX4AniCxpW0lwUSzkcO80nAru1xU5RQr
=belp
-----END PGP SIGNATURE-----

#7 lore raw
In reply to Kevin Korb
Quoted 2 lines · Kevin Korb
$ echo -n testing
- -n testing

in POSIX the '-n' switch (and -e) is undefined. you can work around this via hijacking the call:

#!/bin/sh
echo()

{
	case "$1" in
		'-n')
			shift
			printf '%s' "$@"
		;;
		*)
			printf '%s\n' "$@"
		;;
	esac
}

echo foo
echo -n bar

if you really want to remove all the bashisms, it can be a lot of work. dont blame dash for this, but the script author. also consider using shellsheck.net for this with the correct shebang.

bye, bastian

#4 lore raw
In reply to Kevin Korb
Quoted 7 lines · Kevin Korb
Quoted 2 lines
Quoted 2 lines

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

If dash refuses to supply a compatible version of echo then dash must not include a builtin version of echo. If dash didn't have a broken embedded version of echo I wouldn't have a problem.

dash just claims to be compatible with POSIX, not with *sh. If you look for something like this (lighter than bash but with many of its features), glance rather at mksh:

https://www.mirbsd.org/mksh.htm

Now, I don't understand the present issue, all seems just work as expected here (0.5.8):

 $ dash -c 'type echo; echo -n foo; echo foo'
 echo is a shell builtin
 foofoo

++
Seb.

#10 lore raw
In reply to Kevin Korb
Quoted 20 lines · Kevin Korb

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:
echo [-n] args...
Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a newline is output following the arguments.

However, in actual usage:
% dash

$ echo testing
testing
$ echo -n testing
- -n testing
$ /bin/echo -n testing
testing$
Quoted 2 lines

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

Hmm, where does the 0.5.8.2 version come from, I wonder? The latest release is 0.5.8 [0] and I cannot reproduce your issue with it on Fedora [1] (and note that, afaik, Fedora doesn't apply any special patches that could affect this behavior).

Try rebuilding dash with USE=-libedit and see if it changes anything. If not -- I would bug the Gentoo maintainers.

P

[0] http://gondor.apana.org.au/~herbert/dash/
[1] dash-0.5.8-3.fc23.x86_64

author
#8 lore raw
In reply to Bastian Bittorf

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Then why is the -n behaviour documented in the man page?

Quoted 11 lines · Bastian Bittorf
Quoted 1 line · Kevin Korb
$ echo -n testing - -n testing

in POSIX the '-n' switch (and -e) is undefined. you can work around this via hijacking the call:

#!/bin/sh echo() { case "$1" in '-n') shift printf '%s' "$@" ;; *) printf '%s\n' "$@" ;; esac }

echo foo echo -n bar

Quoted 9 lines

if you really want to remove all the bashisms, it can be a lot of work. dont blame dash for this, but the script author. also consider using shellsheck.net for this with the correct shebang.

bye, bastian -- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZEujcACgkQVKC1jlbQAQfIuQCeJrtFu94ThY+48k5zphx+s1i/ mJAAn2zHqfppmvh7mCrWuXHJGnkq9XJ6
=pqKh
-----END PGP SIGNATURE-----

author
#5 lore raw
In reply to Seb

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This is what I get:
% dash -c 'type echo; echo -n foo; echo foo'
echo is a shell builtin
- -n foo
foo

Quoted 18 lines · Seb
Quoted 6 lines · Kevin Korb
Quoted 2 lines
Quoted 2 lines

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

If dash refuses to supply a compatible version of echo then dash must not include a builtin version of echo. If dash didn't have a broken embedded version of echo I wouldn't have a problem.

dash just claims to be compatible with POSIX, not with *sh. If you look for something like this (lighter than bash but with many of its features), glance rather at mksh:

https://www.mirbsd.org/mksh.htm

Now, I don't understand the present issue, all seems just work as expected here (0.5.8):

Quoted 9 lines
$ dash -c 'type echo; echo -n foo; echo foo' echo is a shell
builtin foofoo

++ Seb.

-- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZEumwACgkQVKC1jlbQAQciqwCeNRjcqFps3ZNtisqM3UI2rlQn 3NMAn2DAvJuycgyFnDTf10jP3y4Gu8x9
=lCLu
-----END PGP SIGNATURE-----

author
#11 lore raw
In reply to Petr Šabata

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

AHA! Now we are getting somewhere. I compiled dash myself and it behaved properly. I then tracked my problem to this Gentoo patch:
https://gitweb.gentoo.org/repo/gentoo.git/tree/app-shells/dash/files/das
h-0.5.8.1-dumb-echo.patch

So, apparently my problem is with Gentoo not dash. Thanks for helping me figure that out.

Quoted 22 lines · Petr Šabata
Quoted 10 lines · Kevin Korb

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:
echo [-n] args... Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a newline is output following the arguments.

Quoted 5 lines

However, in actual usage: % dash $ echo testing testing $ echo -n testing - -n testing $ /bin/echo -n testing testing$

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

Hmm, where does the 0.5.8.2 version come from, I wonder? The latest release is 0.5.8 [0] and I cannot reproduce your issue with it on Fedora [1] (and note that, afaik, Fedora doesn't apply any special patches that could affect this behavior).

Quoted 8 lines

Try rebuilding dash with USE=-libedit and see if it changes anything. If not -- I would bug the Gentoo maintainers.

P

[0] http://gondor.apana.org.au/~herbert/dash/ [1]
dash-0.5.8-3.fc23.x86_64

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZEu9wACgkQVKC1jlbQAQes8gCaA39BdrLiTABBFuiehKwIus0/ jDEAoNQuoxqXSR8FSxJxPS6UrG7qZIUB
=mtMj
-----END PGP SIGNATURE-----

author
#12 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

For future reference, I did some reading in Gentoo's bugzilla...

Apparently the Gentoo devs broke dash's echo -n on purpose. Their intention is to cause any shell script that is called with /bin/sh to fail if the script uses echo -n. However, they didn't limit this behaviour to dash as /bin/sh as it happens as /bin/dash too and they didn't modify the man page and they didn't break bash this way.

So, I am calling this a stupid Gentooism and I am forking their ebuild for my own systems with that patch commented out.

Quoted 3 lines · Kevin Korb

AHA! Now we are getting somewhere. I compiled dash myself and it behaved properly. I then tracked my problem to this Gentoo patch:
https://gitweb.gentoo.org/repo/gentoo.git/tree/app-shells/dash/files/d

as

Quoted 2 lines

h-0.5.8.1-dumb-echo.patch

Quoted 32 lines

So, apparently my problem is with Gentoo not dash. Thanks for helping me figure that out.

Quoted 26 lines · Petr Šabata
Quoted 10 lines · Kevin Korb

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1

I am on dash version 0.5.8.2 on Gentoo Linux (USE=libedit -static).

The echo builtin does not work as described. In the man page:
echo [-n] args... Print the arguments on the standard output,
separated by spaces. Unless the -n option is present, a newline is output following the arguments.

Quoted 5 lines

However, in actual usage: % dash $ echo testing testing $ echo -n testing - -n testing $ /bin/echo -n testing testing$

This is causing me problems when I attempt to switch /bin/sh from bash to dash.

Hmm, where does the 0.5.8.2 version come from, I wonder? The latest release is 0.5.8 [0] and I cannot reproduce your issue with it on Fedora [1] (and note that, afaik, Fedora doesn't apply any special patches that could affect this behavior).

Try rebuilding dash with USE=-libedit and see if it changes anything. If not -- I would bug the Gentoo maintainers.

P

Quoted 8 lines
Quoted 2 lines

[0] http://gondor.apana.org.au/~herbert/dash/ [1]
dash-0.5.8-3.fc23.x86_64

-- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

- -- ~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,
Kevin Korb Phone: (407) 252-6853
Systems Administrator Internet:
FutureQuest, Inc. [email protected] (work)
Orlando, Florida [email protected] (personal)
Web page: http://www.sanitarium.net/
PGP public key available on web site.
~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._.,-*~'^'~*-,._., -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZExH8ACgkQVKC1jlbQAQcWQACg92JXp/RNRl5Bw3qGeFzQF8hq aXIAoK13wl6ELE9BEfFjRd1qJoWk26lC
=xFAp
-----END PGP SIGNATURE-----

#6 lore raw
In reply to Kevin Korb
2015-11-11 22:46:02 -0500, Kevin Korb:

[...]

Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

NOTE: your shell may have its own version of echo, which usually super‐ sedes the version described here. Please refer to your shell's docu‐ mentation for details about the options it supports.

By the way, 'echo -n' is non-portable, and POSIX says you should use 'printf' instead.

If dash refuses to supply a compatible version of echo then dash must not include a builtin version of echo. If dash didn't have a broken embedded version of echo I wouldn't have a problem.

[...]

To clarify, a few things, sh and the POSIX/Unix utilities like C nowadays is a language that has a (well several) standard that defines it, and several existing implementations of interpreters that interpret that language.

Among the most recognised specifications, we have POSIX and Unix. Both specifications have now been merged. To simplify, the Unix spec is like the POSIX spec but where a few optional parts are required.

So basically we have the POSIX syntax for sh and utilities and the Unix syntax.

In the POSIX syntax, the behaviour for:

echo -n whatever

is unspecified. That means that a POSIX conformant script, or a script written to be portable to POSIX conformant systems must not call echo with a first argument that may be -n. We're told to use printf instead there:

printf %s "$var"
instead of
echo -n "$var"

Same goes for any argument that contains backslash character.

In the Unix syntax however, the behaviour of echo is clearly specified.

echo -n whatever

is required to output "-n whatever<newline>", so your dash is both POSIX and Unix conformant in that regard.

The builtin echo of bash, by default is neither POSIX nor Unix conformant unless both the xpg_echo and posix (that one enabled when called as sh) options have been enabled.

More details at:

http://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo

In short, don't use echo.

Signature · 1 line
Stephane
#9 lore raw
In reply to Kevin Korb
Quoted 1 line · Kevin Korb

Then why is the -n behaviour documented in the man page?

Because '-n' works with 'dash'.
Undefined means: it depends on the implementation.

bye, bastian