Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.5k
  • 205
  • 1.8k
  • 2.3k
Source Link
dimmech
  • 481
  • 2
  • 8
  • 16

Why does the printf statement in this loop output the array out of sequence?

When I execute the following code, the output of the printf statement appears to show array out of sequence. In the declare statement the Source item is declared before Destination and in the output it's reversed.

YELLOW=$'\e[93m' declare -A OP=( [Description]="remote to destination" [Source]="/var/www" [Destination]="/foo/bar" [Log]="my.log" [Email]="me@here" ); NO_COLS=`tput cols` COLS_PER_COL=$(($NO_COLS/3)) PRINT_FORMAT="%"$COLS_PER_COL"s%""s\n" for i in "${!OP[@]}"; do printf $PRINT_FORMAT "$i :" " $YELLOW${OP[$i]}$ENDCOL" done ; 

Output looks like

 Description : remote to destination Destination : /foo/bar Source : /var/www Log : my.log Email : me@here 

Can anyone tell me what is happening here? or how I can achieve the proper order according to the array declaration?