coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: /bin/echo -- $var


From: Stephane Chazelas
Subject: Re: /bin/echo -- $var
Date: Thu, 15 Aug 2019 17:36:44 +0100
User-agent: NeoMutt/20171215

2019-08-15 07:29:37 -0700, Kaz Kylheku (Coreutils):
> On 2019-08-15 00:53, Harald Dunkel wrote:
> > IMHO they should have kept the "no args allowed" for echo
> > ("in the late 70s") and should have introduced a new tool
> > "eecho" instead.
> 
> Well, new tool for printing was introduced under the name "printf".
[...]

And "print" long before that (1983) as a builtin of the ksh
shell which was to be the replacement for sh, and would probably
have been if it hadn't been sold separately from the base system
at a hefty price (it eventually was in spirit as the POSIX sh
specification is largely based on it).

"print" has the same problem as the PWB/SysIII/XSI echo in that
escape sequences are expanded by default (and implementations
handle octal sequences the C/normal way, and some the echo way).
But at least it can be disabled with "-r" and there is an
end-of-option marker:

print -r - "$@"

pdksh, zsh and ksh93 also support --. Sometime after ksh86a, --
was also supported there, but actually any number of "-"
(possibly intersperced with decimal digits) act an
end-of-options marker (like print --1-1-1-- "$@", still works
with /usr/xpg4/bin/sh on Solaris).

That's like zsh's:

echo -E - "$@"

I beleive "printf" was a POSIX invention. It was never added to
ksh88, nor pdksh (though some pdksh-derived shells now have
one). But it's ubiquitous if only as a standalone utility, so I
agree it should be the command to use to output arbitrary text.

ksh93 and zsh added a "printf" builtin and "print -f". bash has
printf but not print.

printf does support "--" (to allow formats that start with -).

A few anoying things with it:

printf '%s\n'
prints the same thing as:
printf '%s\n' ''

So we can't use it to print an arbitrary number of lines (zsh's
print -l has the same problem).

The equivalent of echo "$@" is painful to write:

(IFS=" "; printf '%s\n' "$*")

And it outputs the same thing if "$@" is an empty list or a
list of one empty element. csh echo is the only one that
addresses that one (echo alone doesn't output anything, echo ''
outputs an empty line).

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]