【问题标题】:variable substitution in bash from different source files [duplicate]来自不同源文件的bash中的变量替换[重复]
【发布时间】:2021-02-24 21:52:13
【问题描述】:

我在bash 中有一小段代码

# below variable coming from a source file
test_name_tgt_tbl='abc_123'

# this variable coming from another source --> source 2
red_test='test_name'

# Based on source2 variable I need to find value in source 1
out_put=`echo ${red_test}_tgt_tbl`

echo "target=`echo $out_put`"

我得到的结果如下

target=test_name_tgt_tbl

我想要的结果如下

target=abc_123

我怎样才能做到这一点

【问题讨论】:

  • foo=`echo $bar` 是写foo=$bar 的一种非常糟糕的方式,顺便说一句。

标签: linux bash shell


【解决方案1】:

您可以在 bash 中通过编写 ${!some_var_name} 来实现该间接性。以下是我的做法:

var_name=${red_test}_tgt_tbl
echo ${!var_name}

打印abc_123

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2014-02-02
    • 2021-08-01
    • 2017-12-26
    • 1970-01-01
    相关资源
    最近更新 更多