24

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.

user123444555621
  • 148,182
  • 27
  • 114
  • 126
NeonGlow
  • 1,659
  • 4
  • 18
  • 36
  • 1
    Though this question has not been asked the exact same way, there are multiple answers, for example: http://stackoverflow.com/a/7776409/27862 – user123444555621 Dec 05 '12 at 05:16

4 Answers4

43

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"
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • Thank you so much for a rather surprising one... [sadly, I don't understand how this works] :) – NeonGlow Dec 05 '12 at 07:03
  • 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. – gniourf_gniourf Dec 05 '12 at 07:11
  • 3
    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.) –  May 27 '16 at 11:05
  • I've looked at a few man pages for printf(1) & printf(3) and haven't seen a description of the "-v" flag – iletras Jan 25 '17 at 15:17
  • 1
    @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](https://www.gnu.org/software/bash/manual/bashref.html#index-printf). – gniourf_gniourf Jan 25 '17 at 16:10
  • Be careful. If your CONFIG_VALUE contains any of the following characters, you'll need to escape them for printf: '%', '\', '\n', '\r', '\t' and a few others. – Javon Nov 01 '20 at 04:33
  • 1
    @Javon: fixed! `:)` – gniourf_gniourf Nov 01 '20 at 22:31
22

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
Kousha
  • 1,575
  • 14
  • 18
3

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?

Leonard Pauli
  • 2,662
  • 1
  • 23
  • 23
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.