linux poison RSS
linux poison Email

Bash Script: Find and replace string within string

Below is a simple bash script which will find and replace the substring withing the string. Feel free to use or modify this script.

Source: cat stringreplace.sh
#!/bin/bash

echo -n "Enter main string: "
read string

echo -n "Enter the sub-string that you want to replace: "
read old

echo -n "Enter new sub-string that you want to replace with: "
read new

echo "The new string is now: ${string/$old/$new}"

Result: ./stringreplace.sh 
Enter main string: linuxpoison
Enter the sub-string that you want to replace: poison
Enter new sub-string that you want to replace with: os
The new string is now: linuxos



0 comments:

Post a Comment

Related Posts with Thumbnails