How to replace a placeholder or word in a variable with a value from another variable in Bash?

I am trying to write a simple bash script. I have a simple template variable:

template = "my*appserver" 

Then I have a function ( get_env() ) that returns the values dev , qa or live . I would call get_env , and then replace the template string with the get_env variable get_env return value and replace it with an asterisk. So:

 # Returns "dev" server = get_env # Prints "mydevappserver" template = string_replace(server, template) 

Or:

 # This time, it returns "live" server = get_env # Prints "myliveappserver" template = string_replace(server, template) 

What should I use instead of this function string_replace() to do the binding?

+22
bash shell replace
Dec 04 '12 at 19:53
source share
7 answers

Bash can do string replacements on its own:

 template='my*appserver' server='live' template="${template/\*/$server}" 

For more information on replacing strings, see the extended bash scripting guide .

So for the bash function:

 function string_replace { echo "${1/\*/$2}" } 

And use:

 template=$(string_replace "$template" "$server") 
+50
Dec 04
source share

Replacing strings in bash - a script can, for example, be achieved by sed:

 template=$(echo $template | sed 's/old_string/new_string/g') 

This will replace old_string with new_string in the template variable.

+29
Dec 04
source share

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" # replace the place-holder by 'whatever-you-like': server="whatever-you-like" printf -v template "$template" "$server" 

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 "$@" } # but then, use as: string_replace destination_variable "$template" "$server" 
+5
Dec 04
source share

Yes, or "sed," as others have said, or start with your template in 2 separate variables and build "on the fly." eg.

 templateprefix="my" templatesuffix="appserver" server=get_env template=${templateprefix}${server}${templatesuffix} 
+3
Dec 04 '12 at 19:59
source share

Based on @Spencer Rathbun answer, this can also be done as follows:

  function string_replace { #DOC: "${string/match/replace}" string=$1 echo "${string/$2/$3}" } template='my__patternReplacing__appserver' match='__patternReplacing__' replace='live' template=$(string_replace "$template" "$match" "$replace") 
+1
Oct 19 '15 at 7:57
source share

I needed to do something similar, but I needed sed and a replace clause to include the variable. I am done with this.

If the variable name is $ puttyline

 sed "s/\(\[remote .origin.\]\)/\1\n$puttyline/" 

So sed searches for [remote "origin"] and remembers the match, and inserts a line immediately after that containing something in the $puttyline variable.

This may be ugly, but if $ puttyline contains any special characters that sed would respond to, for example \ or $. You have to either run away from them to another sed call, or ... do something smarter in bash that I'm too shitty with bash to know. For example, to double-escape all backslashes:

 sed 's/\\/\\\\/g' 
0
Oct 07 '13 at 3:50
source share

Here's a bash script that wraps this

Example:

 $ search_and_replace.sh "test me out" "test me" ez ez out 

search_and_replace.sh

 #!/bin/bash function show_help() { echo "" echo "usage: SUBJECT SEARCH_FOR REPLACE_WITH" echo "" echo "eg " echo "" echo "test ta => aesa" echo "'test me out' 'test me' ez => ez out" echo "" exit } if [ "$1" == "help" ] then show_help exit fi if [ -z "$3" ] then show_help exit fi SUBJECT=$1 SEARCH_FOR=$2 REPLACE_WITH=$3 echo "$SUBJECT" | sed -e "s/$SEARCH_FOR/$REPLACE_WITH/g" 
0
Feb 16 '16 at 18:09
source share



All Articles