Introduction
You use string replacement to change parts of text stored in a variable quickly without extra tools.
${variable/old/new}text="hello world" echo "${text/world/universe}"
filename="report.txt" echo "${filename/txt/pdf}"
sentence="one fish two fish" echo "${sentence/fish/cat}"
sentence="one fish two fish" echo "${sentence//fish/cat}"
#!/bin/bash text="I love apples and apples are tasty" # Replace first 'apples' with 'oranges' new_text="${text/apples/oranges}" echo "$new_text" # Replace all 'apples' with 'oranges' all_replaced="${text//apples/oranges}" echo "$all_replaced"
${var/old/new} do in bash scripting?animal?text="hello world world", what is the output of echo ${text/world/earth}?file="data.txt"
newfile=${file/data/txt}
echo $newfilesentence="apple banana apple cherry apple". How do you replace only the second occurrence of 'apple' with 'orange' using bash string replacement?Pass the test and this pattern moves unknown