简体   繁体   English

替换修饰符中的参数扩展

[英]Parameter expansion in substitution modifier

Let x be a parameter initialized as follows:x是一个初始化如下的参数:

x=("drink/beer" "drink/whine" "drink/boooze")

Suppose there are also parameters y and z , with y="drink" amd z="buy" , respectively.假设还有参数yz ,分别为y="drink" amd z="buy" For each word in x , I want to substitute the portion that machtes y with z .对于x每个单词,我想用z替换 machtes y的部分。 Expected output , hence, is因此,预期输出为

buy/beer buy/whine buy/boooze

I though the following code would do so:我虽然下面的代码会这样做:

print ${x:s/${y}/${z}}

However, it does not work.但是,它不起作用。 The "drink" substrings are not substituted and I could find the reason in themodifier documentation : “饮料”子字符串未被替换,我可以在修饰符文档中找到原因:

The s/l/r/ substitution works as follows [...] When used in a history expansion, which occurs before any other expansions, l and r are treated as literal strings s/l/r/ 替换的工作方式如下 [...] 在历史扩展中使用时,发生在任何其他扩展之前,l 和 r 被视为文字字符串

This means, that in my attempt zsh literally searchs for "${y}", which is of course not to be found.这意味着,在我的尝试中,zsh 从字面上搜索“${y}”,这当然是找不到的。 Is there another way the accomplish my goal, without using an explicite loop?有没有另一种方法可以在不使用显式循环的情况下实现我的目标?

Parameter expansion has its own replacement syntax, separate from the history modification operators.参数扩展有自己的替换语法,与历史修改操作符分开。

% print ${x/$y/$z}
buy/beer buy/whine buy/boooze

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM