Skip to content

Instantly share code, notes, and snippets.

@shivergard
Forked from coderofsalvation/str_replace.bash
Created September 1, 2014 11:35
Show Gist options
  • Save shivergard/66f32afad5e4f4c679c5 to your computer and use it in GitHub Desktop.
Save shivergard/66f32afad5e4f4c679c5 to your computer and use it in GitHub Desktop.
# replaces a string with another string
# @param string source string
# @param string search string
# @param string replacement string"
# usage: result="$(str_replace "foo bar" "foo" "hi")"
function str_replace()
{
[ -z "$1" ] && return 1
[ -z "$2" ] && return 1
[ -z "$3" ] && return 1
_strreplace_var_replace=$(printf "%s\\n" "$1" | sed -e "s/${2}/${3}/g")
printf "%s\\n" "${_strreplace_var_replace}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment