[sclug] Escaping slashes in bash

Matt matt at bodgit-n-scarper.com
Tue Sep 27 18:20:00 UTC 2005


* David Kerrawn (sclug) <sclug at kerrawn.co.uk> [2005-09-27 18:34:57]:
> > -----Original Message-----
> > From: sclug-bounces at sclug.org.uk [mailto:sclug-bounces at sclug.org.uk]On
> > Behalf Of Peter Brewer
> > Sent: 27 September 2005 17:29
> > To: sclug at sclug.org.uk
> > Subject: [sclug] Escaping slashes in bash
> > 
> > 
> > Dear SCLUG
> > 
> > Having some problems with escaping "/" in Bash.  We want to run perl 
> > -pie in a bash script but have had to first put the substitution string 
> > into a variable otherwise it doesn't get created properly.  This works 
> > fine when there are no "/" in the substitution string but how do we get 
> > it to work if there IS a "/"????
> > 
> > e.g:
> > WORKDIR="/tmp"
> > LOCALITIESFILE="foobar.txt"
> > CMD="s/\[LOCALITIESFILE]/${WORKDIR}\/${LOCALITIESFILE}/g"
> > `perl -p -i -e ${CMD} ${FILENAME1}`
> > 
> > Many thanks once again for your combined help!
> > 
> > Cheers
> > 
> > Pete
> > 
> 
> I think WORKDIR="\\/tmp" should do what you want.
> 
> $ WORKDIR="\\/tmp"
> $ echo $WORKDIR
> \/tmp
> $ CMD="s/\[LOCALITIESFILE]/${WORKDIR}\/${LOCALITIESFILE}/g"
> $ echo $CMD
> s/\[LOCALITIESFILE]/\/tmp\/foobar.txt/g

Alternatively, to avoid any backslash-escaping mess, why not simply use
a different delimiter for the s/// operator? For example:

s|foo|bar|g

Although humour me, what is the original example supposed to be doing
anyway?

Matt
-- 
"I know it's your T1 because our network guy teleported into the Baywatch
hub and checked it!"
"It's 'telnet' and 'Bay Networks'."
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
Url : http://lists.tmdg.co.uk/pipermail/sclug/attachments/20050927/3c4de4eb/attachment.bin


More information about the Sclug mailing list