如何插入变量与bash的另一个变量 [英] How to insert a variable into another variable with bash

查看:128
本文介绍了如何插入变量与bash的另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本的问题。

I have a problem with a bash script.

我想一个变量插入一个bash脚本另一个变量,但risult是不是我应该期待什么。

I would like to insert a variable into another variable in a bash script, but the risult is not what I should expect.

在这里,code

input1="inputnumber1"
input2="inputnumber2"
input3="inputnumber3"
dummy="input"
for i in $(seq 1 3)
do
    toprint=$dummy$i
    echo "$toprint"
done

我希望这code打印变量$输入1的内容,$输入2和$输入3,但它只是打印输入1,输入2和输入3。

I would expect this code print the content of the variable $input1, $input2 and $input3, but it just print input1, input2 and input3.

任何建议?

先谢谢了。

推荐答案

使用间接变量引用:

varname="$dummy$i"
toprint="${!varname}"

这篇关于如何插入变量与bash的另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆