A small template can use a visible placeholder that the script replaces with a selected value. This keeps the fixed text separate from the changing value.

Program

Play the script to choose the deployment target and see the template filled in.

target
placeholder_replace.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

target="staging"
template="deploy __TARGET__"
command="${template/__TARGET__/$target}"
echo "$command"
#!/usr/bin/env bash

target="production"
template="deploy __TARGET__"
command="${template/__TARGET__/$target}"
echo "$command"
  1. target ← staging

    3target="staging"4template="deploy __TARGET__"
    values this stepstagingtarget
  2. template ← deploy __TARGET__

    3target="staging"4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"
    values this stepdeploy __TARGET__template
  3. command ← deploy staging

    4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"6echo "$command"
    values this stepdeploy stagingcommanddeploy __TARGET__templatestagingtarget
  4. echo "$command"

    5command="${template/__TARGET__/$target}"6echo "$command"
    outputdeploy staging
    values this stepdeploy stagingcommand
  1. target ← production

    3target="production"4template="deploy __TARGET__"
    values this stepproductiontarget
  2. template ← deploy __TARGET__

    3target="production"4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"
    values this stepdeploy __TARGET__template
  3. command ← deploy production

    4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"6echo "$command"
    values this stepdeploy productioncommanddeploy __TARGET__templateproductiontarget
  4. echo "$command"

    5command="${template/__TARGET__/$target}"6echo "$command"
    outputdeploy production
    values this stepdeploy productioncommand
placeholder A placeholder marks the part of a template that will change.
parameter substitution `${text/pattern/value}` replaces the first matching pattern in a string.
template boundary Keeping fixed text in `template` makes the changing value easier to review.