0

I'm trying to export variables inside of a for loop where I wanted each iteration of the loop to change what variable was being written to. I've been looking for the right syntax to satisfy exporting like this in bash, but none of I've tried have worked. What is the proper way to do this?

To add more detail, my hope is when the loop runs, I would be exporting FVAR0 on the first run, FVAR1 on the second, FVAR2 on the third, and so on.

Originally I had tried export FVAR$i

export "${!FVAR[$i]}" is my most recent guess.

#!/bin/bash
set -x
for i in 0 1 2 3 4
do
    export "${!FVAR[$i]}"=$(ls ./run/$i)
done


10:36:10 Thu Jan 24 ::  ./uploader.sh
+ for i in 0 1 2 3 4
++ ls ./run/0
+ export =
./uploader.sh: line 5: export: `=': not a valid identifier
+ for i in 0 1 2 3 4
++ ls ./run/1
+ export =
./uploader.sh: line 5: export: `=': not a valid identifier
+ for i in 0 1 2 3 4
++ ls ./run/2
+ export =
./uploader.sh: line 5: export: `=': not a valid identifier
+ for i in 0 1 2 3 4
++ ls ./run/3
+ export =
./uploader.sh: line 5: export: `=': not a valid identifier
+ for i in 0 1 2 3 4
++ ls ./run/4
+ export =
./uploader.sh: line 5: export: `=': not a valid identifier
  • Do you not have an array called FVAR? This is what you're using. – Kusalananda Jan 24 at 15:47
  • I've edited my question. I'm hoping to get the loop to change which FVAR is being written to. There should be no "FVAR" but there should be FVAR0 FVAR1 FVAR2 etc – Volumetricsteve Jan 24 at 15:54
  • 1
    It looks like you actually do want to use an array here. Is there a reason that you want to create environment variables rather than ordinary shell variables? What are you planning to use the ls output for (there are much better ways to get file listings, that preserve whitespaces etc. in filenames). – Kusalananda Jan 24 at 15:58
  • Well this is where it gets dicey. Explained here, exporting arrays won't happen: stackoverflow.com/questions/5564418/… – Volumetricsteve Jan 24 at 16:01
  • Don't pay any mind to what's going into the variable, all I need is the syntax to allow it to export. lt was just a simple place holder so I could cleanly post the question. – Volumetricsteve Jan 24 at 16:02
0

This works:

#!/bin/bash

for x in 0 1 2 3; do
    export FVAR$x="Is's $x"
done

env | grep FVAR

Executing:

$ ./fvar.sh 
FVAR3=Is's 3
FVAR2=Is's 2
FVAR1=Is's 1
FVAR0=Is's 0
  • That's bizarre, a coworker of mine and I have both tried that, as I stated above, that's the very first thing we tried. Is $i reserved? The only differences I see between yours and mine is you used 'x' instead, and you put a semi-colon in your for loop, neither of which seem to matter since I back-ported your change to my loop, now it's fine. Thank you for the sanity-reinforcement. – Volumetricsteve Jan 24 at 18:40
0
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...

  • Thank you for the additional information! – Volumetricsteve Jan 24 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.