如何在变量中嵌套变量?

时间:2011-08-09 22:41:20

标签: linux unix shell ksh

我有一个场景,我需要执行以下语句。

air sandbox run $AI_PLAN/Sam_22.plan

对于上述命令,应该从命令中获取AI。

> echo $PREFIX
AI

我尝试了以下方式

air sandbox run $`echo $PREFIX`_PLAN/Sam_22.plan

返回错误:File not found

dollar_prefix=$`echo $PREFIX`
air sandbox run ${dollar_prefix}_PLAN/Sam_22.plan

返回错误:File not found

请告诉我上述编码在哪里出错。

3 个答案:

答案 0 :(得分:1)

你需要eval一些东西:

PREFIX=AI
AI_PLAN=some_directory
eval directory=\$${PREFIX}_PLAN
air sandbox run $directory/Sam_22.plan

答案 1 :(得分:0)

尝试使用命令替换:

$(command ...)_PLAN

让我们假设程序make_prefix在您的路径中。然后:

> make_prefix
AI
> echo $(make_prefix)
AI
> echo $(make_prefix)_PLAN
AI_PLAN

答案 2 :(得分:0)

不清楚你真正想要发生什么,但我猜你可能想要像

这样的东西
air sandbox ${${PREFIX}_PLAN}/Sam_22.plan

这将获取变量PREFIX(AI在您的情况下?)的值并附加_PLAN,并使用THAT作为要获取的变量的名称