radas
radas

Reputation: 357

How to capture the nested variable in for loop?

> cat script    
copy1=/tmp/a.txt    
copy2=/tmp/b.txt    
dest1=/tmp/c.txt    
dest2=/tmp/d.txt    
for i in `seq 1 4` do ; cp -pr $copy${i}  $dest${i}
done

Is the above possible? I wasn't able to copy file to dest files.

Upvotes: 1

Views: 44

Answers (1)

anubhava
anubhava

Reputation: 785531

You can use variable reference:

for ((i=1; i<=4; i++)); do
   src="copy$i"
   dest="dest$i"
   cp -pr "${!src}" "${!dest}"
done

Upvotes: 1

Related Questions