在UNIX中:
我有一些变数:
FOLDER0=/home/user0
FOLDER1=/home/user1
FOLDER2=/home/user2
FOLDER3=/home/user3
FOLDER=FOLDER
for i in 0 1 2 3
do
${FOLDER}${i} // print /home/user0, /home/user1 and so on based on value of i
done
$ {i} $ {FOLDER}的值应根据i的值打印/ home / user0,/ home / user1等 有没有办法实现这个目标?
修改 将数字放在变量的末尾而不是开头。
答案 0 :(得分:2)
使用eval
:
#!/usr/bin/bash
FOLDER0=/home/user0
FOLDER1=/home/user1
FOLDER2=/home/user2
FOLDER3=/home/user3
FOLDER=FOLDER
for i in {0..3}; do
eval var=\${FOLDER${i}}
print $var
done
注意:我的循环是特定于bash / ksh的,但eval
结构不是。