Skip to main content
added 3 characters in body
Source Link
tshepang
  • 67.7k
  • 94
  • 226
  • 297

You can use placeholders for string templates instead of unexpanded variables. This will get messy pretty quickly. If what you are doing is very template heavy, you may want to consider a language with a real template library.

format_template() {
    changed_str=$1
    
    for word in $changed_str; do
        if [[ $word == %*% ]]; then
            var="${word//\%/}"
            changed_str="${changed_str//$word/${!var}}"
        fi
    done
}

str1='I went to %PLACE% and saw %EVENT%'
PLACE="foo"
EVENT="bar"
format_template "$str1"
echo "$changed_str"

The downside to the above is thethat the template variable must be it's own word (eg you can't do "%prefix%foo""%prefix%foo"). This could be fixed with some modifications, or simply by hard-coding the template variable instead of it being dynamic.

You can use placeholders for string templates instead of unexpanded variables. This will get messy pretty quickly. If what you are doing is very template heavy, you may want to consider a language with a real template library.

format_template() {
    changed_str=$1
    
    for word in $changed_str; do
        if [[ $word == %*% ]]; then
            var="${word//\%/}"
            changed_str="${changed_str//$word/${!var}}"
        fi
    done
}

str1='I went to %PLACE% and saw %EVENT%'
PLACE="foo"
EVENT="bar"
format_template "$str1"
echo "$changed_str"

The downside to the above is the the template variable must be it's own word (eg you can't do "%prefix%foo"). This could be fixed with some modifications, or simply by hard-coding the template variable instead of it being dynamic.

You can use placeholders for string templates instead of unexpanded variables. This will get messy pretty quickly. If what you are doing is very template heavy, you may want to consider a language with a real template library.

format_template() {
    changed_str=$1
    
    for word in $changed_str; do
        if [[ $word == %*% ]]; then
            var="${word//\%/}"
            changed_str="${changed_str//$word/${!var}}"
        fi
    done
}

str1='I went to %PLACE% and saw %EVENT%'
PLACE="foo"
EVENT="bar"
format_template "$str1"
echo "$changed_str"

The downside to the above is that the template variable must be it's own word (eg you can't do "%prefix%foo"). This could be fixed with some modifications, or simply by hard-coding the template variable instead of it being dynamic.

Source Link
jordanm
  • 43.6k
  • 10
  • 121
  • 115

You can use placeholders for string templates instead of unexpanded variables. This will get messy pretty quickly. If what you are doing is very template heavy, you may want to consider a language with a real template library.

format_template() {
    changed_str=$1
    
    for word in $changed_str; do
        if [[ $word == %*% ]]; then
            var="${word//\%/}"
            changed_str="${changed_str//$word/${!var}}"
        fi
    done
}

str1='I went to %PLACE% and saw %EVENT%'
PLACE="foo"
EVENT="bar"
format_template "$str1"
echo "$changed_str"

The downside to the above is the the template variable must be it's own word (eg you can't do "%prefix%foo"). This could be fixed with some modifications, or simply by hard-coding the template variable instead of it being dynamic.