Вопрос

I know that I can use indirect expansion to call variables whose names are contained in other variables as follows

VAR="test"

VARNAME="VAR"

echo ${!VARNAME}

However if I try to redefine a variable with this:

VARVALUE=0 VALUE="VARVALUE"

${!VARNAME}=${!VALUE}

echo ${!VARNAME}

It doesnt work, and I get

bash: test=0: command not found

I can see why this variable declaration fails, but I can't see how to fix it. In searching I've only found examples calling variables with indirect expansion, but not defining them so.

edit:

After a bit more searching, I've tried

eval "${!VARNAME}=${!VALUE}"

which throws

bash: =0: command not found

Это было полезно?

Решение

Fiddling with it some more, I've managed to find a solution

eval "$VARNAME=${!VALUE}"

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top