从另一个字符串的值创建字符串变量名称 [英] Creating a string variable name from the value of another string

查看:21
本文介绍了从另一个字符串的值创建字符串变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 bash 脚本中,我有两个变量 CONFIG_OPTIONCONFIG_VALUE,它们分别包含字符串 VENDOR_NAMEDefault_Vendor.

In my bash script I have two variables CONFIG_OPTION and CONFIG_VALUE which contain string VENDOR_NAME and Default_Vendor respectively.

我需要创建一个名为 $CONFIG_OPTION 的变量,即 VENDOR_NAME 并将 CONFIG_VALUE 中的值分配给新创建的变量.

I need to create a variable with name $CONFIG_OPTION ie VENDOR_NAME and assign the value in CONFIG_VALUE to newly created variable.

我该怎么做?

我试过了

$CONFIG_OPTION=$CONFIG_VALUE

但我在这一行收到错误

'./Build.bash: line 137: VENDOR_NAME="Default_Vendor": command not found'

谢谢.

推荐答案

我知道没有人会提到它,所以我开始了.您可以使用 printf!

I know that nobody will mention it, so here I go. You can use printf!

#!/bin/bash

CONFIG_OPTION="VENDOR_NAME"
CONFIG_VALUE="Default_Vendor"

printf -v "$CONFIG_OPTION" "%s" "$CONFIG_VALUE"

# Don't believe me?
echo "$VENDOR_NAME"

这篇关于从另一个字符串的值创建字符串变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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