Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

replace string in variable

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
kim_wei
Asked:
kim_wei
  • 3
  • 2
1 Solution
 
liddlerCommented:
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
 
kim_weiAuthor Commented:
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
 
liddlerCommented:
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
 
kim_weiAuthor Commented:
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
 
liddlerCommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now