Substitution in a variable

Hey All,

I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works fine. Here's my code...

#!/bin/sh

var_name="Hello World"
echo "var_name is $var_name"
var_name=`echo $var_name |sed 's/ /\\ /g'`
echo "Cat_name has changed to $Cat_name"

Thanks,
G

First thing I noticed, was that you would echo out $Cat_name. You didn't define Cat_name. The other thing I did was to add another \ to the sed statement, along with the -e option... Hope that helps...

Try this:

#!/bin/sh

var_name="Hello World"
echo "var_name is $var_name"
var_name=`echo $var_name | sed -e 's/ /\\\ /g'`
echo "var_name has changed to $var_name"

Hi,

just change as below :
var_name=`echo $var_name|sed 's/ / \//g'`

or for backslash :
var_name=`echo $var_name|sed 's/ / \\\/g'`

krishna

Sweet it works! Thanks!