dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

for loop

oceanMIH opened this issue · comments

for ((i=0;i<${#arr[@]};i++)); do
printf '%s\n' "${arr[i]}"
done

the above code may be wrong if the array is not contiuous, or there are holes in the array
for example:
arr=(apple [3]=banana [6]=cherry)

You can recreate array:

arr2=("${arr[@]}")

You can recreate array:

arr2=("${arr[@]}")

yes, thanx
I think the following is better:
for i in ${!arr[@]}; do
printf '%s\n' "${arr[i]}"
done

Also should work without loop:

printf '%s\n' "${arr[@]}"