Bug#490605: debian-policy: please discourage the usage of echo -n, and echo in general
Russ Allbery wrote:
Raphael Hertzog <firstname.lastname@example.org> writes:
On Sat, 12 Jul 2008, Raphael Geissert wrote:
As demonstrated by the following trivia, and also mentioned by
SUSv3, the echo built-in varies from implementation to implementation
and thus should be discouraged.
Well, you just proved that you can't rely on correct interpretation of
special escaped characters and that's true. It's not a reason to
discourage echo or echo -n in general though. That would be ridiculous
given how frequently it's used.
But it would be nice to remember that printf "" must be used if you want
to reliably use escaped characters. But here you must take care to use
"%s" for each variable expansion:
echo "a $var\n b"
printf "a %s\n b\n" "$var"
I realize that Policy already provides a little bit of generic advice
about writing shell scripts, so it's not like we have a particularly pure
distinction on which to stand, but unless we're going to require
particular practices via filing bugs I think putting best practices in the
devref may be better.
Policy does say by reference that echo varies. It referes to SUS, which
If the first operand is -n, or if any of the operands contain a
backslash ( '\' ) character, the results are implementation-defined.
It is not possible to use echo portably across all POSIX systems
unless both -n (as the first argument) and escape sequences are
The printf utility can be used portably to emulate any of the
traditional behaviors of the echo utility as follows (assuming that
IFS has its standard value or is unset): [...]
We of course override the part about -n and require that it behave in a
particular way, but the part about backslashes remains.
It's unfortunate that SUS requires free registration; it makes it harder
to link directly to specific sections of interest.
The POSIX pages are available online, in http://kerneltrap.org/man/linux
unfortunately they are not very well formatted (some extra groff
are written on html)
I don't think this bug need to be address, for the following reasons:
- I don't find debian scripts which uses escapes (but one of mine
package in stable, on the non-debian support part)
Eventually policy could state that escapes should not be used
- "-n" is so wide used, that other solution will create more bugs!
Anyway, no user should use "echo -n" to print "-n" (POSIX
discurages it), so again, it is a non-problem.
note: it can is very easy corrected by wrapper (or shell alias)
- I think the debian script are is outside POSIX scope:
- the system is not completely up, so POSIX commands and
support is incomplete.
Anyway the init.d scripts use usually other non-portable
command (i.e. mount), so
- the installation of program is also outside POSIX scope:
it modify system in a manner not allowed by portable
So, IMHO, there is noway to have debian script compatible
100% to posix.
So I would reject the bug, until we don't see real problems.
OTOH moving to LSB command could help in this case, help logging
and give some colors to users.