The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-11-2007 02:44 AM
help in replacing ?? zedex Shell Programming and Scripting 5 08-31-2007 01:45 AM
replacing variable values in all files in directories newtoshell Shell Programming and Scripting 3 12-30-2005 04:11 PM
Replacing set of characters with a value of a variable prekida Shell Programming and Scripting 1 07-26-2005 08:14 PM
Replacing pattern in variable videsh77 Shell Programming and Scripting 1 05-26-2005 02:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-29-2008
ashish_uiit ashish_uiit is offline
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
  #2 (permalink)  
Old 02-29-2008
ashish_uiit ashish_uiit is offline
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.
  #3 (permalink)  
Old 02-29-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
you need to quote to avoid expansion, and your sed is a little out:

Code:
 echo '$DESTDIR/$PKG/$VERSION' | sed -e "s/\\$/XXX/g"
XXXDESTDIR/XXXPKG/XXXVERSION
  #4 (permalink)  
Old 02-29-2008
sank sank is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 24
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.
  #5 (permalink)  
Old 02-29-2008
ashish_uiit ashish_uiit is offline
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.
  #6 (permalink)  
Old 02-29-2008
sank sank is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 24
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 ?
  #7 (permalink)  
Old 02-29-2008
ashish_uiit ashish_uiit is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0