如何用Bash中另一个变量的值替换变量中的占位符字符或单词?

我正在尝试编写一个简单的Bash脚本。我有一个简单的“模板”变量:
template = "my*appserver"

然后我有一个函数(get_env())返回值dev,qa或live。我想调用get_env,然后使用get_env的返回值将模板变量替换为string,并将其与星号交换掉。所以:

# Returns "dev"
server = get_env

# Prints "mydevappserver"
template = string_replace(server,template)

要么:

# This time,it returns "live"
server = get_env

# Prints "myliveappserver"
template = string_replace(server,template)

我应该使用什么来代替这个string_replace()函数来完成绑定?

Bash可以自行更换字符串:
template='my*appserver'
server='live'
template="${template/\*/$server}"

有关字符串替换的更多详细信息,请参见advanced bash scripting guide

所以对于bash函数

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

并使用:

template=$(string_replace "$template" "$server")

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...