[sclug] Escaping slashes in bash

David Kerrawn sclug sclug at kerrawn.co.uk
Tue Sep 27 17:34:17 UTC 2005


> -----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

Regards
Davek.




More information about the Sclug mailing list