Skip to main content
added 445 characters in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441
export "${!FVAR[$i]}"=...

This would assume FVAR is an array, and try to use its element in index $i as a name of a variable to expand on that command line. (That is, a=11; b=22; c=33; p=(a b c); echo ${!p[1]} would print 22 since p[1] is b, $b is 22.)

FVAR[0] etc. don't hold names of variables in that script, so that expansion results in an empty string.

You should be able to use just

for i in 1 2 3;3 4 5; do
    export "FVAR$i=$(generate some value from $i)"
done

as export processes the variable name after expansions. This is not the case with a regular assignment, however. FVAR$i=$(...) doesn't work.


Of course, depending on what you're doing, there might be other ways, like sticking all the values together in one variable, and exporting that.

FVARS="$(generate some value from 1)"
for i in 2 3 4 5 ; do
    FVARS+=":$(generate some value from $i)"
done
export FVARS

Though that requires reserving some character to use as a separator, and arranging to put the separator in the right place etc...

export "${!FVAR[$i]}"=...

This would assume FVAR is an array, and try to use its element in index $i as a name of a variable to expand on that command line. (That is, a=11; b=22; c=33; p=(a b c); echo ${!p[1]} would print 22 since p[1] is b, $b is 22.)

FVAR[0] etc. don't hold names of variables in that script, so that expansion results in an empty string.

You should be able to use just

for i in 1 2 3; do
    export "FVAR$i=$(generate some value from $i)"
done

as export processes the variable name after expansions. This is not the case with a regular assignment, however. FVAR$i=$(...) doesn't work.

export "${!FVAR[$i]}"=...

This would assume FVAR is an array, and try to use its element in index $i as a name of a variable to expand on that command line. (That is, a=11; b=22; c=33; p=(a b c); echo ${!p[1]} would print 22 since p[1] is b, $b is 22.)

FVAR[0] etc. don't hold names of variables in that script, so that expansion results in an empty string.

You should be able to use just

for i in 1 2 3 4 5; do
    export "FVAR$i=$(generate some value from $i)"
done

as export processes the variable name after expansions. This is not the case with a regular assignment, however. FVAR$i=$(...) doesn't work.


Of course, depending on what you're doing, there might be other ways, like sticking all the values together in one variable, and exporting that.

FVARS="$(generate some value from 1)"
for i in 2 3 4 5 ; do
    FVARS+=":$(generate some value from $i)"
done
export FVARS

Though that requires reserving some character to use as a separator, and arranging to put the separator in the right place etc...

Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

export "${!FVAR[$i]}"=...

This would assume FVAR is an array, and try to use its element in index $i as a name of a variable to expand on that command line. (That is, a=11; b=22; c=33; p=(a b c); echo ${!p[1]} would print 22 since p[1] is b, $b is 22.)

FVAR[0] etc. don't hold names of variables in that script, so that expansion results in an empty string.

You should be able to use just

for i in 1 2 3; do
    export "FVAR$i=$(generate some value from $i)"
done

as export processes the variable name after expansions. This is not the case with a regular assignment, however. FVAR$i=$(...) doesn't work.