LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed with bash variables (https://www.linuxquestions.org/questions/linux-software-2/sed-with-bash-variables-304260/)

MurrayL 03-21-2005 07:52 AM

sed with bash variables
 
I am trying to run sed from a bash script to replace a value in a string with a bash variable.
Essentially,
Code:

MYVAR="cabbage"
sed -e 's/lettus/$MYVAR/'

But this dosn't work (presumably because the sed command is contained in apostrophies so the variables is not translated) all lettuses get replaced by "$MYVAR" rather than "cabbage"

Can anyone suggest how I can get this to work?

druuna 03-21-2005 08:06 AM

Hi,

From within a (bash) script you need to use double quotes instead of singel qoutes to expand the variable. I.e:

Code:

MYVAR="cabbage"
sed -e "s/lettus/$MYVAR/"

Hope this helps.

slackie1000 03-21-2005 08:13 AM

Re: sed with bash variables
 
Quote:

Originally posted by MurrayL
I am trying to run sed from a bash script to replace a value in a string with a bash variable.
Essentially,
Code:

MYVAR="cabbage"
sed -e 's/lettus/$MYVAR/'

But this dosn't work (presumably because the sed command is contained in apostrophies so the variables is not translated) all lettuses get replaced by "$MYVAR" rather than "cabbage"

Can anyone suggest how I can get this to work?

just complementing druuna information, you can also single-quote the variable :
Code:

sed -e 's/lettus/'$MYVAR'/'
should work also.

regards

slackie1000


All times are GMT -5. The time now is 06:09 PM.