otaolafr
otaolafr

Reputation: 15

use array in bash for change name of created folder in each loop

I am trying to do a script in bash and the idea is that I have an array with text and I use to creat a folder, for example:

#!/bin/bash
dir=(dir1 dir2 dir3)
for i in 0 1 2; do
    d=run_${#dir[i]}
    echo "Prepare case ${d}..."
done

My problem is that when I do this, it prints:

Prepare case run_4...
Prepare case run_4...
Prepare case run_4...

and the number 4 corresponds to the lenght of each array element, (if I change dir1 to direc1 for example, I get in the first line of the output "Prepare case run_6...")

What i was looking:

Prepare case run_dir1...
Prepare case run_dir2...
Prepare case run_dir3...

What i am missing?

Upvotes: 1

Views: 38

Answers (1)

Ivan
Ivan

Reputation: 7287

Remove # from this commnd d=run_${#dir[i]}

And you can loop over array values like this:

for i in "${dir[@]}"; do
    echo "Prepare case run_$i..."
done

Upvotes: 1

Related Questions