Kannan Lg
Kannan Lg

Reputation: 921

merge variables in unix

In UNIX:

I have a few variables:

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

The value of ${i}${FOLDER} should print /home/user0, /home/user1 and so on based on value of i Is there any way to achieve this?

EDIT: Put the numbers at the end of the variable instead of at the beginning.

Upvotes: 0

Views: 86

Answers (1)

Tim Pote
Tim Pote

Reputation: 28029

Use 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

Note: my loop was bash/ksh-specific, but the eval construct is not.

Upvotes: 2

Related Questions