LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-21-2005, 08:52 AM   #1
MurrayL
LQ Newbie
 
Registered: Feb 2004
Location: South Africa
Distribution: RedHat 9.0
Posts: 22

Rep: Reputation: 15
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?
 
Old 03-21-2005, 09:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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.
 
1 members found this post helpful.
Old 03-21-2005, 09:13 AM   #3
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
using bash variables in sed jimieee Programming 6 02-04-2004 05:11 AM
bash - sed/tr??? pk21 Programming 2 09-05-2003 08:56 AM
passing variables to sed jjfate Programming 8 07-31-2003 05:15 AM
bash - sed pk21 Programming 6 03-07-2003 12:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration