How to generate environment variable name dynamically and export it
You could use printf -v
to create variables dynamically, for example:
temp=somename
echo $temp
printf -v $temp "Test value"
echo $somename
This will output "Test value".
Note that temp="$(date +%s)"
won't work, because the output of $(date +%s)
is numeric, and variable names in Bash cannot start with a number. You would have to give it a non-numeric prefix, for example:
temp="t$(date +%s)"
To export the variable, you can simply do:
export $temp
Here's a complete example, with proof that the variable really gets exported in the environment:
temp=t$(date +%s)
echo $temp
printf -v $temp "Test value"
export $temp
sh -c "echo \$$temp"
Outputs for example:
t1486060416
Test value