Bash字符串格式化/替换

时间:2013-05-29 20:15:12

标签: bash

我在bash脚本中有2个变量,如下所示:

key='fox'

string='The quick brown jumps over the lazy dog'

我想创建一个新变量,在keybrown之间插入jumps。我怎么能用bash做到这一点?我正在尝试这样的事情,但我无法让它发挥作用:

sentence='The quick brown ${key} jumps over the lazy dog'

句子的变量应该是:

string='The quick brown fox jumps over the lazy dog'

1 个答案:

答案 0 :(得分:5)

你可以写:

sentence="The quick brown ${key} jumps over the lazy dog"

使用双引号而不是单引号。 (参数扩展和所有其他替换在单引号内被禁用;请参阅§3.1.2.2 "Single Quotes" in The Bash Reference Manual。)

相关问题