sed 's/foobar/$string/g'.... can't do this?

Maarten Litmaath maart at cs.vu.nl
Sat Feb 2 07:26:21 AEST 1991


In article <1069 at mwtech.UUCP>,
	martin at mwtech.UUCP (Martin Weitzel) writes:
)[...]
)One loophole still remains even in Geoff's solution: embedded newlines in
)$string. [...]

Here's an example that deals with everything, as far as I know.
Enjoy.

--------------------cut here--------------------
#!/bin/sh
# sed(1) safe substitute example

newline='
'

string='this string contains an embedded newline,
a backslash, a slash, an ampersand and 2 terminating newlines: \ / &

'

nl=

case $string in
*$newline)
	nl=$newline
esac

Xstring=`sed -e 's-[/&\\\\]-\\\\&-g' -e '$!s/$/\\\\/' <<!
$string
!
`$nl

sed "s/.*/$Xstring/" <<!
hello
!
--
Temporary files like /tmp/sh$$ are an abomination.



More information about the Comp.unix.shell mailing list