我正在尝试在 for 循环中导出变量,我希望循环的每次迭代都可以更改要写入的变量。我一直在寻找正确的语法来满足在 bash 中这样的导出,但我尝试过的都没有奏效。这样做的正确方法是什么?
添加更多细节,我希望当循环运行时,我会在第一次运行时导出 FVAR0,在第二次运行时导出 FVAR1,在第三次运行时导出 FVAR2,依此类推。
本来我试过export FVAR$i
export "${!FVAR[$i]}"
是我最近的猜测。
#!/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