In a script i have a function which evaluates passed 'key=value' parameters, i.e.:
function evaluateKeyValuePair() {
eval "$1"
}
evaluateKeyValuePair "key=value with whitespaces"
evaluateKeyValuePair "key=value"
The latter call works fine, "echo $key" prints "value". However calling the function with whitespaces in "value" does not work: a command-not-found error is thrown for "with" and "whitepaces"
I've already read that using eval generally is a bad idea. But, unfortunately i cannot change the basic layout of the function(s). I just have to live with it.
I tried to change it to:
function evaluateKeyValuePair() {
key="${1%%=*}"
val2="${1#*"="}"
eval $key="$val2"
}
but this does not work either.
Is there a way to evalute key-pair-values with whitespaces in "value" or do i have to test for whitespaces in the passed parameter and return an error if there are any?
THX in advance!