You are not logged in.

#1 2011-03-02 05:27:10

tjwoosta
Member
Registered: 2008-12-18
Posts: 453

[solved] How to avoid expanding variables with sed?

Stupid question probably..

Say I have a script that uses the variable $HOME, and I want to replace it with /another/dir using sed

I try something like..

sed -i "s_$HOME/file_/another/dir/file_g" script.sh

but its a no go, I think sed expands $HOME to /home/tj and fails to find it in the script. How to avoid this?

Last edited by tjwoosta (2011-03-02 05:57:06)

Offline

#2 2011-03-02 05:34:46

escondida
Package Maintainer (PM)
Registered: 2008-04-03
Posts: 157

Re: [solved] How to avoid expanding variables with sed?

You want to wrap the s/ expression in single quotes, then. That's not sed, though, but bash. This should prove enlightening (-:

Offline

#3 2011-03-02 05:56:46

tjwoosta
Member
Registered: 2008-12-18
Posts: 453

Re: [solved] How to avoid expanding variables with sed?

Thanks smile

Offline

#4 2011-03-03 03:51:49

escondida
Package Maintainer (PM)
Registered: 2008-04-03
Posts: 157

Re: [solved] How to avoid expanding variables with sed?

Glad to help!

Offline

#5 2011-03-03 13:25:25

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [solved] How to avoid expanding variables with sed?

alternatively you could escape the $, if you wanted to mix some expanded vars and some not:

sed -i "s_\$HOME/file_/another/dir/file_g" script.sh

That should also work.

Offline

Board footer

Powered by FluxBB