debug_echo_array

Function description

The debug_echo_array function may be used to selectively output the contents of an array, including indices and values, to stderr and / or the system event log.

Function parameters

$1 - The array variable to be output.

Global variables

$DEBUG - If this global variable is set then output will be sent to stderr.

$LOGGING - If this global variable is set then output will be sent to the system event log.

Example use

Listing 1
  1. #! /bin/bash
  2.  
  3. # We rely on some functions from hacking-bash.sh
  4. [[ -r ${HACKING_BASH_LIB_PATH:=/usr/lib/hacking-bash.sh} ]] && \
  5.     source ${HACKING_BASH_LIB_PATH} || \
  6.     { echo "Unable to find ${HACKING_BASH_LIB_PATH}"; exit 1; }
  7.  
  8. # Create an example array
  9. ARRAY[0]="zero"
  10. ARRAY[1]="one"
  11. ARRAY[4]="four"
  12.  
  13. # Set the DEBUG flag, we want output to the terminal
  14. DEBUG="true"
  15.  
  16. # Use the debug_echo_array function
  17. debug_echo_array ARRAY
Example code demonstrating the debug_echo function
max@lisa ./debug_echo_array.sh
debug: ARRAY={[0]="zero", [1]="one", [4]="four"}