配列に含まれる内容に繰り返し置き換える

配列に含まれる内容に繰り返し置き換える
foo bar, foo bar, foo bar
foo bar

barこの美しい詩を考えると、それらをすべて置き換えることができますが、superman配列に含まれて毎回異なるエンディングを持つことはどのように可能ですか?(is super, is blazing fast, is marvelous, cawabanga)

結果は次のとおりです。

foo superman is super, foo superman is blazing fast, foo superman is marvelous
foo superman cawabanga

for/loop と sed を使用してこれを行うことができることを知っています。しかし、より短く、審美的により満足のいく解決策がありますか?

ベストアンサー1

これは、シェルパラメータ拡張によって簡単に実行できます。これは実際には簡単な説明です。

poem='foo bar, foo bar, foo bar
foo bar'
traits=( "is super" "is blazing fast" "is marvelous" "cawabanga" )

for trait in "${traits[@]}"; do poem=${poem/bar/"superman $trait"}; done

echo "$poem"
foo superman is super, foo superman is blazing fast, foo superman is marvelous
foo superman cawabanga

おすすめ記事