Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created January 8, 2014 20:45
Show Gist options
  • Save coderofsalvation/8324279 to your computer and use it in GitHub Desktop.
Save coderofsalvation/8324279 to your computer and use it in GitHub Desktop.
replaces a string with another string (bash function)
# 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