The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing a number with random variable inside shell script user_prady Shell Programming and Scripting 3 09-10-2007 11:44 PM
help in replacing ?? zedex Shell Programming and Scripting 5 08-30-2007 10:45 PM
replacing variable values in all files in directories newtoshell Shell Programming and Scripting 3 12-30-2005 01:11 PM
Replacing set of characters with a value of a variable prekida Shell Programming and Scripting 1 07-26-2005 05:14 PM
Replacing pattern in variable videsh77 Shell Programming and Scripting 1 05-26-2005 11:11 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
Replacing $ in variable

hi
I have a variable like

k=$DESTDIR/$PKG/$VERSION

I want to replace each $ in string k with say "XXX".
so that k becomes like this "XXXDESTDIR/XXXPKG/XXXVERSION"

when I use echo $k | sed -e "s/\$/XXX" it actually passes expanded of variables $DESTDIR, $PKG and $VERSION to sed.

Please help.

Ashish
Reply With Quote
Forum Sponsor
  #2  
Old 02-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
Replacing "$" in variable

hi
I have a variable like

k=$DESTDIR/$PKG/$VERSION

I want to replace each $ in string k with say "XXX".
so that k becomes like this "XXXDESTDIR/XXXPKG/XXXVERSION"

when I use echo $k | sed -e "s/\$/XXX" it actually passes expanded of variables $DESTDIR, $PKG and $VERSION to sed.


Please help.
Reply With Quote
  #3  
Old 02-29-2008
Registered User
 

Join Date: Feb 2008
Posts: 19
probably the variable k is not formed correctly. Can you try echo $k and see if you get $DESTDIR/$PKG/$VERSION ? Are you forming the variable k like this : k="\$DESTDIR/\$PKG/\$VERSION" ? if so, the sed command works as expected.
Reply With Quote
  #4  
Old 02-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
Quote:
Originally Posted by sank View Post
probably the variable k is not formed correctly. Can you try echo $k and see if you get $DESTDIR/$PKG/$VERSION ? Are you forming the variable k like this : k="\$DESTDIR/\$PKG/\$VERSION" ? if so, the sed command works as expected.
$DESTDIR/$PKG/$VERSION is coming from a read only file to k while reading.
Reply With Quote
  #5  
Old 02-29-2008
Registered User
 

Join Date: Feb 2008
Posts: 19
then, you need to see how to read it into k by escaping the $. can you tell us how you are reading it from the file and assigning to k ?
Reply With Quote
  #6  
Old 02-29-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
I am using while loop like

while read k

.....// if $DESTDIR is present in k then go to next line in file
because DESTDIR is not defined .
else
use value of k as it is it may be $PKG/$VERSION
as these variable are defined.

.....
done < $file
Reply With Quote
  #7  
Old 02-29-2008
Registered User
 

Join Date: Feb 2008
Posts: 19
Quote:
Originally Posted by ashish_uiit View Post
I am using while loop like

while read k

.....// if $DESTDIR is present in k then go to next line in file
because DESTDIR is not defined .
else
use value of k as it is it may be $PKG/$VERSION
as these variable are defined.

.....
done < $file
when you do 'echo $k', do you see the shell variables expanded ?
I feel that you need to escape $ by adding '\' everytime you append it to k.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0