Thursday, April 26, 2012

Search and replace in Bash scripting

Search and replace is common requirement in any scripting. Bash provides simple way to replace strings/characters in variable.

The bash syntax for search and replace is


S1- Source string
S2- String to search
S3 - String to replace with

Lets see simple example.

Example1:  Replace all "/" with "\"

[bash] # var="/home/product/module/lib"
[bash] # var=${var//\//\\} [bash] # echo $var \home\product\module\lib

Example2:  Replace all "product" with "src"

[bash] # var="/home/product/module/lib"
[bash] # var=${var//product/src} [bash] # echo $var /home/src/module/lib [bash] #

Pin It
Related Posts Plugin for WordPress, Blogger...