Template Generation
Placeholder Replace
Fill One Slot
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.
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"
target ← staging
3target="staging"4template="deploy __TARGET__"values this stepstagingtargettemplate ← deploy __TARGET__
3target="staging"4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"values this stepdeploy __TARGET__templatecommand ← deploy staging
4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"6echo "$command"values this stepdeploy stagingcommanddeploy __TARGET__templatestagingtargetecho "$command"
5command="${template/__TARGET__/$target}"6echo "$command"outputdeploy stagingvalues this stepdeploy stagingcommand
target ← production
3target="production"4template="deploy __TARGET__"values this stepproductiontargettemplate ← deploy __TARGET__
3target="production"4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"values this stepdeploy __TARGET__templatecommand ← deploy production
4template="deploy __TARGET__"5command="${template/__TARGET__/$target}"6echo "$command"values this stepdeploy productioncommanddeploy __TARGET__templateproductiontargetecho "$command"
5command="${template/__TARGET__/$target}"6echo "$command"outputdeploy productionvalues 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.