dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.
MIT License
36.41k stars 3.27k forks source link

for loop #138

Closed oceanMIH closed 1 year ago

oceanMIH commented 1 year ago

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)

andry81 commented 1 year ago

You can recreate array:

arr2=("${arr[@]}")
oceanMIH commented 1 year ago

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

MegaV0lt commented 1 year ago

Also should work without loop:

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