Creating a string variable name from the value of another string

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"

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

Tags:

Unix

Bash