Skip to main content
added detail
Source Link

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

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?

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

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
Source Link

How can I export a variable in bash where the variable name is comprised of two variables?

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?

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