Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!accuvax.nwu.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: Regular Expression delimiters Message-ID: <16874@mimsy.UUCP> Date: 12 Apr 89 05:02:29 GMT References: <993@n8emr.UUCP> <6710@bsu-cs.bsu.edu> Distribution: comp Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 45 >In article <6710@bsu-cs.bsu.edu> dhesi@bsu-cs.bsu.edu (Rahul Dhesi) >suggests something like >> pattern="`echo "$ans" | sed -e 's/\//\\\\\\//g'`" In article tale@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@mimsy.umd.edu Path: uunet!mimsy!chris