[comp.unix.questions] echo

mouse@mcgill-vision.UUCP (der Mouse) (12/21/86)

In article <198@haddock.UUCP>, karl@haddock.UUCP (Karl Heuer) writes:
> The three major branches [of UNIX] (Research, USG, and Berkeley) have
> distinct versions of echo!

> Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
> Btw, the V8 echo is the best of the lot; I hope the others adopt it.

What are the differences?  Why do you feel V8 echo is best?  (I know,
it's the most nutritious.  Tastes best too. :-)

					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     think!mosart!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
ARPAnet: think!mosart!mcgill-vision!mouse@harvard.harvard.edu

karl@haddock.UUCP (Karl Heuer) (12/24/86)

[This article will undoubtedly provoke a dozen identical postings of McIlroy's
humorous discussion of "echo", from Kernighan & Pike.  Please don't.]

In article <585@mcgill-vision.UUCP> mcgill-vision!mouse (der Mouse) writes:
>In article <198@haddock.UUCP>, karl@haddock.UUCP (Karl Heuer) writes:
>> The three major branches [of UNIX] (Research, USG, and Berkeley) have
>> distinct versions of echo!  ...
>> Btw, the V8 echo is the best of the lot; I hope the others adopt it.
>
>What are the differences?  Why do you feel V8 echo is best?

Berkeley echo is the same as Research V7.  It knows one flag ("-n", suppress
newline); anything else is echoed literally.  The only thing that's hard to
echo is a literal "-n" including newline.

USG echo (SysIII, SysV) doesn't expect any flags, but interprets backslash
escapes similar to C (except that they botched backslash-octal-digits, and
they added "\c" to suppress the newline).  This is a bad implementation of a
good idea.  (It's nice to be able to manipulate control characters without
having to embed them literally in the shell script, but it would've been more
useful to have it somehow built into the shell rather than each individual
program (echo, tr, stty, etc.).*)  The problem with this approach is that it
is now difficult to use echo to echo things!  Shell scripts that try to echo
variables into pipes (echo "$1" | sed ...) can have unexpected results.

Research V8 echo has the backslash escape feature, but they made it an option
which is off by default ("-e" enables it).  They also implemented backslash-
-octal-digits correctly (the same notation as in C), and they kept the -n flag
(so "echo -n foo" and "echo -e 'foo\c'" are equivalent).  One thing they might
have overlooked (I don't have a man page or a V8 system available; this is
from memory) is a way to forcibly terminate option scanning.  (With the USG
function getopt(), this is automatically provided via "--".  This notation is
probably as good as any.)  If this option is/were included, then one can/could
be sure of echoing something exactly by using `echo -- "$1"'.  This would also
allow for future additions**, e.g. an option to echo multiple arguments on
separate lines, without breaking existing scripts (provided they use "--").

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
*I know "echo" is a builtin in ksh and SVR2 sh.  That's irrelevant, since it's
semantically identical to /bin/echo.  I'm talking about a feature that would
make `echo "$TERMCAP"' work literally (in spite of backslashes contained in
the variable) while `echo "\n"' (or some other notation) would pass a newline
in argv[1].  Now, backslash is already recognized inside double quotes, and so
it should be safe to add this as written; unfortunately, many shell scripts
depend on the current behavior and use a single backslash (instead of two)
when a real backslash is intended.  Perhaps "$\n" would be a safe notation?

**I dislike creeping featurism (Berkeley cat(1) is the classical example), but
`for i do echo "$i"; done' is annoying, ugly, and slow (compared to the
desired `echo -m "$@"').  Anyway, why isn't this the default behavior?  What
good are those embedded spaces on output, except for the benefit of people too
lazy to enclose a sentence in quotes?

ccplumb@watnot.UUCP (01/09/87)

In article <287@haddock.UUCP> karl@haddock.ISC.COM.UUCP (Karl Heuer) writes:
-   One thing they might have overlooked (I don't have a man page or a
- V8 system available; this is from memory) is a way to forcibly terminate
- option scanning.  (With the USG function getopt(), this is automatically
- provided via "--".  This notation is probably as good as any.)  If this
- option is/were included, then one can/could be sure of echoing something
- exactly by using `echo -- "$1"'.  This would also allow for future
- additions, e.g. an option to echo multiple arguments on separate lines,
- without breaking existing scripts (provided they use "--").

  My two cents' worth...  an "echo rest of line raw" flag is definitely
the way to go... although I'd use the null flag ("-") for the purpose.

  Echo implementors, take note!

	-Colin Plumb (ccplumb@watnot.UUCP)

Zippy says:
Four thousand different MAGNATES, MOGULS & NABOBS are romping in my
gothic solarium!!

karl@haddock.UUCP (Karl Heuer) (01/12/87)

 if it appears in the context of an input
file.  Since echo doesn't read file names, "-" could be used safely, but it's
easier to remember if it's consistent with the rest of the world.  (In SysV,
at least, an awful lot of programs have been converted to use getopt, and I
suspect it or something a lot like it is the wave of the future.)

Btw, "echo rest of line raw" is slightly ambiguous.  My meaning was "stop
parsing options, even if the next argument begins with a dash"; this should be
orthogonal to other "cooking" like backslash interpretation, space separation,
newline termination, etc.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

chris@mimsy.UUCP (Chris Torek) (10/17/88)

In article <1577@hp-sdd.HP.COM> allyn@hp-sdd.hp.com (Allyn Fratkin) writes:
>chars=`echo '\040\011\014\c'`	# use -n with bsd echo

The 2BSD and 4BSD programs correctly do *not* interpret `\' escape
sequences by default.  (Of course, they also do not ever do it....)
V8 and V9 echo use `-e' to enable escapes.  Berkeley will no doubt
follow whatever POSIX says.

(Rather than mucking with echo, USG should have supplied printf(1).)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

aad@stpstn.UUCP (Anthony A. Datri) (10/21/88)

In article <14024@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>The 2BSD and 4BSD programs correctly do *not* interpret `\' escape
>sequences by default.  (Of course, they also do not ever do it....)

I find it a pain, but I also find the lit program from comp.sources.unix
to be a good replacement.


-- 
@disclaimer(Any concepts or opinions above are entirely mine, not those of my
	    employer, my GIGI, or my 11/34)
beak is@>beak is not
Anthony A. Datri @SysAdmin(Stepstone Corporation) stpstn!aad

chris@mimsy.UUCP (Chris Torek) (04/13/89)

In article <7702@phoenix.Princeton.EDU> bernsten@phoenix.Princeton.EDU
(Dan Bernstein) writes:
>sh, if you have a working echo (whose only caveat is -n):
>  pattern="`(echo -n \"$ans\";echo '') ...
>  NOTE: Chris, want to take back that ``no workaround''?

Okay, make that `no sane workaround' :-)

More seriously: This fixes the bogosity in the BSD echo, but not the
SysV echo.  Sigh.  (Oh well, at least this makes another argument for
the `4.x > V' side :-) )

Incidentally, /bin/echo works properly with no arguments (produces
a blank line), unlike csh's built-in echo (which requires that empty
argument).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

esok@ohs.uucp (Eric Sokolowsky) (02/06/91)

Is there a way to use 'echo' on a c-shell without appending a carriage return
automatically?

-- 
*******Eric Sokolowsky (ohs!ohs-cs!esok@uunet.UU.NET)
November, n.:
	The eleventh twelfth of a weariness.
		-- Ambrose Bierce, "The Devil's Dictionary"

tbecker@salbei.informatik.uni-kl.de (Thomas Becker) (02/07/91)

esok@ohs.uucp (Eric Sokolowsky) writes:

>Is there a way to use 'echo' on a c-shell without appending a carriage return
>automatically?

Yes, there is. Try echo -n <your strings>.
Thomas
-- 
Thomas Becker		     +---------+ acoustic:
Computer Science Department  |  /"""\  |	+49 631 205 3295
University of Kaiserslautern | { O~O } | electronic:
P.O.Box 3049		     |  \ U /  |        tbecker@informatik.uni-kl.de