What I want to do is following. Inside a function, I need to assign a value to a variable, whose name is taken from another variable. In other words:
func() {
#
# Here happens something that ultimately makes $arg="var_name"
#
declare -g ${arg}=5
}
func
echo ${var_name}; # Prints out "5"
The code snippet above works great in bash 4.2. However, in bash before 4.2, declare doesn’t have the -g option. Everything I found at google says that to define the global variable inside a function, I should just use the var=value syntax, but unfortunately var itself depends on another variable. ${arg}=5 doesn’t work, either. (It says -bash: var_name=5: command not found.
For the curious, the reason for all this is that this function actually creates global variables from the script parameters, i.e. running script --arg1=val automatically creates variable named arg1 with value val. Saves tons of a boilerplate code.
You could construct your var=value as a string and evaluate it using the bash builtin command
eval.