25

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

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

How I can do this?

I tried

$CONFIG_OPTION=$CONFIG_VALUE

But I am getting an error on this line as

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

Thanks.

1

4 Answers 4

45

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"
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much for a rather surprising one... [sadly, I don't understand how this works] :)
You can read help printf. The -v option to printf means that it should assign the formated string to the variable the name of which is given just after the -v option.
This does not appear to work in at least some shells (e.g., ash shell). (Although question was specifically for Bash, so it's not a big deal.)
@iletras: -v is a switch for Bash's builtin printf. The documentation is hence found by either: 1. typing help printf in the Bash prompt, 2. reading Bash's man with man bash, 3. reading the online reference manual.
@Javon: fixed! :)
|
23

This uses bash builtins:

#!/bin/bash

VAR1="VAR2"

declare "${VAR1}"="value"

echo "VAR1=${VAR1}"
echo "VAR2=${VAR2}"

The script output:

VAR1=VAR2
VAR2=value

Here's the snippet using your variable names:

#!/bin/bash

CONFIG_OPTION="VENDOR_NAME"

declare "${CONFIG_OPTION}"="value"

echo "CONFIG_OPTION=${CONFIG_OPTION}"
echo "VENDOR_NAME=${VENDOR_NAME}"

The script output:

CONFIG_OPTION=VENDOR_NAME
VENDOR_NAME=value

3 Comments

I find the declare syntax much easier to read than using printf
but declare is bash specific, ergo, not portable.
I enjoy the use of printf in the answer from @gniourf_gniourf, until I saw this answer and remembered this approach, which is much more useful and intuitive for the coders who come after me. :)
4

For pure shell, possibly try:

#!/usr/bin/env sh

option=vendor_name
value="my vendor"

eval $option="'$value'" # be careful with ', \n, and \ in value
eval echo "\$$option" # my vendor
echo "$vendor_name" # my vendor

Why?

#!/usr/bin/env sh
printf -v "var" "val" # prints the flag, var not set
declare var=val # sh: declare: not found
echo ${!var} # sh: syntax error: bad substitution

I don't like eval, but are there any POSIX options?

Comments

1

Existing answers are great for strings, but do not work with arrays. Here is a working solution for arrays:

ARRAY=(a b c d e f)
array_name="ARRAY_NAME"

TARGET_ARRAY="${array_name}[@]"      # ARRAY="ARRAY[@]"
TARGET_ARRAY=("${!TARGET_ARRAY}")    # ARRAY=(a b c d e f)

the array_name string can also have * suffix to obtain the array elements.

1 Comment

I think you meant array_name="ARRAY"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.