2

So I know how to loop through 1 variable:

folder="(first|second|third)"
for i in "${folder[@]}"; do
  rclone move /mounts/$folder/cache
done

But how to do it when they are 2?

folder="(first|second|third)"
mount="(something1|something2|something3)"
for i in "${folder[@]}"; do
  rclone move /mounts/$folder/cache $mount
done

So script should loop each folder with correct mount added.

At the end, execution should be like this:

 rclone move /mounts/first/cache something1
 rclone move /mounts/second/cache something2
 rclone move /mounts/third/cache something3
2

1 Answer 1

7

You need to iterate over the array index number and not the value. This is done using the ${!folder[@]} syntax.

In that way you can access both arrays via the same index inside the loop

e.g. (putting echo in front for this example)

folder=(first second third)
mount=(something1 something2 something3)
for i in "${!folder[@]}"; do
  echo rclone move /mounts/${folder[$i]}/cache ${mount[$i]}
done

Will output

rclone move /mounts/first/cache something1
rclone move /mounts/second/cache something2
rclone move /mounts/third/cache something3

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.