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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace a line content victorcheung Shell Programming and Scripting 13 04-21-2008 08:46 PM
Content of Content of a variable! jaduks Shell Programming and Scripting 2 08-27-2007 12:40 AM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 02:56 PM
replace a string with content from another file afatguy Shell Programming and Scripting 4 09-14-2006 09:25 PM
Compare the content of a variable with a string robdai UNIX for Dummies Questions & Answers 2 11-01-2005 07:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2006
josephwong josephwong is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 4
How to replace a variable content

Hi,

variable1="This is a car"

Now I want to replace the content of variable1, "car" to "dog". Is there any simple command I can use.

Thanks.

Joseph
  #2 (permalink)  
Old 06-23-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,402
You don't say which shell you are using. This is bash...
Code:
variable1=${variable1/car/dog}
  #3 (permalink)  
Old 06-23-2006
josephwong josephwong is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 4
korn shell,

It seems it does not work.

Any suggestions? Thanks.
  #4 (permalink)  
Old 06-23-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
I am not aware of any shell-builtins' (except for the ones Ygor posted) which carries out string replacement.

Try sed

Code:
echo "$variable" | sed -e "s_car_dog_g"
awk can do it as well. Look into the gsub method.
  #5 (permalink)  
Old 06-25-2006
josephwong josephwong is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 4
Thanks. It works.
Sponsored Links
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 09:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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