参数扩展可以嵌套在 Bash 中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6724056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:23:26  来源:igfitidea点击:

Can parameter expansion be nested in Bash?

bash

提问by Tyilo

Possible Duplicate:
Can ${var} parameter expansion expressions be nested in bash?

可能的重复:
${var} 参数扩展表达式可以嵌套在 bash 中吗?

Is it possible to nest the shell parameter expansion (${})?

是否可以嵌套 shell 参数扩展(${})

If I want to do something like this:

如果我想做这样的事情:

foo=( 1 2 3 4 5 )
echo ${${foo[@]/3/bar}:1}

采纳答案by EdoDodo

As far as I know, it is not possible to nest shell parameter expansion. I'm afraid you'll have to figure out another way of achieving what you need. If you post the code, maybe we can help you.

据我所知,嵌套shell参数扩展是不可能的。恐怕你必须想出另一种方式来实现你所需要的。如果您发布代码,也许我们可以帮助您。

回答by Gilles 'SO- stop being evil'

No, you can't. (You can in zsh, but not in bash, ksh or other shells.)

不,你不能。(您可以在 zsh 中,但不能在 bash、ksh 或其他 shell 中。)

You need to use an intermediate variable:

您需要使用一个中间变量:

foo=( 1 2 3 4 5 )
tmp=("${foo[@]/3/bar}")
echo "${tmp[@]:1}"