Solved

replace string in variable

Posted on 2004-10-20
5
198 Views
Last Modified: 2010-04-21
hello, all

  i am coding a linux shell scripts, where a variabe "X1=ABCEDFG" is defined, how can i use other string, like "./",  to replace "X1=" ?

thanx in advance

kim
0
Comment
Question by:kim_wei
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 12358260
Not sure I understand your question.
Do you want a variable called "./" ?
If so I don't think you can as they are reserved metacharacters.
If not can you explain more?
0
 

Author Comment

by:kim_wei
ID: 12359410
hello, liddler,
   
  a piece of code like following,

...
DEF_STR="X1=ABCED"
echo $DEF_STR
...

  i just want to use "./" to replace "X1=", so it would look like,

DEF_STR="./ABCED", hopefully it is clear.

thanx
0
 
LVL 18

Expert Comment

by:liddler
ID: 12359569
you can use sed
sed 's/X1=/\.\//g' script.sh > script1.sh

This will replace the string X1= with ./ wherever it appears in the file script.sh and save the output in script1.sh
0
 

Author Comment

by:kim_wei
ID: 12367286
hello, liddler

   i really don't want to have additional file generated in my code, is it possible to perform this replacement only in scripts scope?

thanx
0
 
LVL 18

Accepted Solution

by:
liddler earned 50 total points
ID: 12367383
No,
but you can do:
sed 's/X1=/\.\//g' script.sh > script1.sh && mv script1.sh script.sh
to rename script1.sh back to script.sh
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question