PDA

View Full Version : [SOLVED] Sed s HOME Variable



huangyingw
October 10th, 2010, 04:41 AM
Hello all,
I want to replace the string "media/smb" with my environment variable "$HOME" with sed,as shown bellow:

x=`echo $1 | sed -e "s/\/media\/smb/$HOME/"`, but it return me with such following errors:

sed: -e expression #1, char 17: unknown option to `s'
How could I handle this?

PC-XT
October 10th, 2010, 06:36 AM
I'm far from an expert, but here are some things I might try:

Does it work without the -e switch?

Maybe try the find command first, like
sed '/foo/ s//bar/' filename
to see if it's something other than the s command. (or just replace with other commands)

Is $1 giving a proper input to pipe?

Some Penguin
October 10th, 2010, 06:54 AM
$HOME probably contains a / and thus needs escaping.

(In Perl, there's \Q$blah\E syntax; I don't know if sed uses the same syntax, but it presumably has an equivalent).

mobilediesel
October 10th, 2010, 07:46 AM
Hello all,
I want to replace the string "media/smb" with my environment variable "$HOME" with sed,as shown bellow:

x=`echo $1 | sed -e "s/\/media\/smb/$HOME/"`, but it return me with such following errors:

sed: -e expression #1, char 17: unknown option to `s'
How could I handle this?

Since $HOME will usually contain a slash "/", use "|" instead of "/" for the sed statement:

x=$(echo $1 | sed -e "s|/media/smb|$HOME|")

Also:
Why is $(...) preferred over `...` (backticks)? (http://mywiki.wooledge.org/BashFAQ/082)

huangyingw
October 17th, 2010, 04:37 AM
Since $HOME will usually contain a slash "/", use "|" instead of "/" for the sed statement:

x=$(echo $1 | sed -e "s|/media/smb|$HOME|")

Also:
Why is $(...) preferred over `...` (backticks)? (http://mywiki.wooledge.org/BashFAQ/082)

Sorry for a late reply for your answer. It works for me. Great thanks.