Skip to main content
I've approved the edit but the plural is actually "indices"
Source Link
Anthony Geoghegan
  • 13.6k
  • 7
  • 62
  • 66

You can access the array indexesindices using ${!array[@]} and the length of the array using ${#array[@]}, e.g. :

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get :

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array indexes using ${!array[@]} and the length of the array using ${#array[@]}, e.g. :

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get :

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array indices using ${!array[@]} and the length of the array using ${#array[@]}, e.g. :

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get :

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array indexindexes using ${!array[@]} and the length of the array using ${#array[@]}, e.g. :

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get :

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array indexes using ${!array[@]} and the length of the array using ${#array[@]}, e.g. :

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get :

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3
additions in response to OP's comments
Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get

0/3
1/3
2/3

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $index/${#array[@]}
done

Note that since bash arrays are zero indexed, you will actually get

0/3
1/3
2/3

If you want the count to run from 1 you can replace $index by $((index+1)). If you want the values as well as the indices you can use "${array[index]}" i.e.

#!/bin/bash

array=( item1 item2 item3 )
for index in ${!array[@]}; do
    echo $((index+1))/${#array[@]} = "${array[index]}"
done

giving

1/3 = item1
2/3 = item2
3/3 = item3
Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175
Loading