fork download
  1. #!/usr/bin/env bash
  2.  
  3. # set up some test data
  4. rundate="180618"
  5. rundate1="180820"
  6. rundate2="Values With Spaces Work Too"
  7.  
  8. # If we know all values are numeric, we can use a regular indexed array
  9. # otherwise, the below would need to be ''declare -A alld=( )''
  10. alld=( ) # initialize an array
  11. for v in "${!rundate@}"; do # using @ instead of * avoids IFS-related bugs
  12. alld[${v#rundate}]=${!v} # populate the associative array
  13. done
  14.  
  15. # print our results
  16. printf 'Full array definition:\n '
  17. declare -p alld # emits code that, if run, will redefine the array
  18. echo; echo "Indexes only:"
  19. printf ' - %s\n' "${!alld[@]}" # "${!varname[@]}" expands to the list of keys
  20. echo; echo "Values only:"
  21. printf ' - %s\n' "${alld[@]}" # "${varname[@]}" expands to the list of values
Success #stdin #stdout 0s 19632KB
stdin
Standard input is empty
stdout
Full array definition:
   declare -a alld=([0]="180618" [1]="180820" [2]="Values With Spaces Work Too")

Indexes only:
 - 0
 - 1
 - 2

Values only:
 - 180618
 - 180820
 - Values With Spaces Work Too