Regular Expression delimiters

Chris Torek chris at mimsy.UUCP
Wed Apr 12 15:02:29 AEST 1989


>In article <6710 at bsu-cs.bsu.edu> dhesi at bsu-cs.bsu.edu (Rahul Dhesi)
>suggests something like
>>    pattern="`echo "$ans" | sed -e 's/\//\\\\\\//g'`"

In article <TALE.89Apr11192859 at imagine.pawl.rpi.edu> tale at pawl.rpi.edu
(David C Lawrence) writes:
>pattern="`echo $ans | sed 'sX/X\\\/Xg'`"

Actually, sed 'sX/X\\/Xg' suffices (two backslashes, not three).
(I also prefer a comma as the separator: sed 's,/,\\/,g'.)

>a) quoting $ans is only necessary if "-n" could lead it.  If you want
>to allow for that you need to quote it as "`echo \"$ans\" ...`"

This makes little difference.  Quoting $ans is necessary if and only
if it contains special characters (space, tab, newline, and globbing).
If the string is "-n", echo will still break; worse, if it contains
backslashes, it will break on SysV, where echo does escape interpretation
(echo should never have acquired a -n flag either; that job should
have been left to printf(1).)

It is true that if "$ans" is "-n foo", then

	echo $ans

produces

	foo

without a newline, while

	echo "$ans"

produces

	-n foo

(with a newline).  But

	echo "-n"

produces nothing.  There is no workaround for this.
-- 
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.questions mailing list