Like no one mentioned this, here's a cool feature using printf . The seat holder must be %s , but not * .
# use %s as the place-holder template="my%sappserver"
Done!
If you want the function to do this (and notice how all the other solutions mentioning the function use the ugly subshell):
#!/bin/bash # This wonderful function should be called thus: # string_replace "replacement string" "$placeholder_string" variable_name string_replace() { printf -v $3 "$2" "$1" } # How to use it: template="my%sappserver" server="whatever-you-like" string_replace "$server" "$template" destination_variable echo "$destination_variable"
Done (again)!
Hope you enjoyed it ... now tailor it to your needs!
Note. This method using printf seems to be a little faster than bash string replacement. And there is no subshell at all! Wow, this is the best method in the West.
It's funny If you like funny things, you can write the string_replace function above as
string_replace() { printf -v "$@" }
gniourf_gniourf Dec 04 2018-12-12T00: 00Z
source share