linux replace string in variable code example
Example 1: linux replace string in all files
sed -i 's/old-text/new-text/g' input.txt
Example 2: bash replace substring
echo [string] | sed "s/[original]/[target]/g"
Example 3: bash script: replace . with :
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
message='The secret code is 12345'
echo "${message//[0-9]/X}"
Example 4: bash replace beginning of string
$ cat shortest.sh
filename="bash.string.txt"
echo ${filename#*.}
echo ${filename%.*}
$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string