Skip to content

Instantly share code, notes, and snippets.

@samir64
Last active May 6, 2018 11:16
Show Gist options
  • Save samir64/e93cc52fd7722c3ef7e83dc37dc9bf75 to your computer and use it in GitHub Desktop.
Save samir64/e93cc52fd7722c3ef7e83dc37dc9bf75 to your computer and use it in GitHub Desktop.
Replace substrings by another strings and return value to first parameter of passed arguments in bash script
function replace_regexp() {
local result=$1
local text=$2
shift
shift
while [[ $# -gt 0 ]]; do
text=$(echo $text | sed -E "s/$1/$2/g")
shift
shift
done
eval $result="'$text'"
}
#replace_regexp res "This is <h1>sample</h1> text" "<\/?h1>" "--"
#echo "$res"
function replace_string() {
local result=$1
local text=$2
shift
shift
while [[ $# -gt 0 ]]; do
text=${text//$1/$2}
shift
shift
done
eval $result="'$text'"
}
#replace_string res "This is <h1>sample</h1> text" "<h1>" "++" "</h1>" "--"
#echo "$res"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment