SysV echo

Chris Torek chris at mimsy.UUCP
Wed May 17 08:58:31 AEST 1989


In article <536 at visdc.UUCP> jiii at visdc.UUCP (John E Van Deusen III) writes:
>Is there a particular SysV echo that is broken, or are you saying that
>System V echo is broken, in general, because, in your opinion, it should
>*not* do escape interpretation?

The latter.  (A separate program---printf(1)---should do escape
interpretation.)

The problem (which echo used to solve before it was changed in SysV or
SysIII [when *did* it acquire interpretation, anyway?]) is this:
suppose you are given a shell variable $foo, and need to echo it
uninterpreted?

There is a solution (albeit ugly):

	cat << end
	$foo
	end

but you may need to do something more.  To keep echo from munching away
backslashes, you might do this:

	qfoo=`cat << end | sed 's/\\\\/\\\\\\\\'
	$foo
	end`

except that, due to vagaries of Bourne's implementation, it does not work.
(It might work in ksh.)  So you get fancier:

	tf=${TMPDIR-/tmp}/quot$$; exec 3>$tf 4<$tf; rm $tf
	cat << end >&3
	$foo
	end
	qfoo=`sed 's/\\\\/\\\\\\\\/g' <&4`
	exec 3>&- 4<&-

and finally you have in $qfoo a version of $foo you can safely hand
to echo(1).  None of this would have been necessary if someone had
simply added printf(1) instead of mucking with echo(1).  Moreover, if
you might want to run your script on (e.g.) V7, you will need a
separate version without the extra quoting code.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list