Function: show

Print, or store in variable, all elements of array

USAGE: show arrayname [-v[ ]varname] [outsep [pre [post]]]

show()
{     
  local var array arrayname outsep pre post
  

If first arg is -v* get name of variable to store result

  case "$1" in
    -v) var=$2; shift 2 ;;
    -v*) var=${1#-v}; shift ;;
  esac
  

Use array whose name is in the first argument

  arrayname=${1:?Array name required}
  

If no separator is supplied, use a newline

  outsep=${2-$'\n'}
  

String to print before each element

  pre=$3
  

String to print after each element

  post=$4
  

Copy the array, $arrayname, to local array

  eval "array=( \"\${$arrayname[@]}\" )"
  

Check whether $var is set

  if [ -z "$var" ]
  

If not, print array to stdout

  then
    printf "$pre%s$post$outsep" "${array[@]}"
    

If $sep doesn't end with a newline and stdout is a terminal, then print a newline

    case $outsep in
      *$'\n') ;;
      *) [ -t 1 ] && echo ;;
    esac
  else
    

... otherwise print output to that variable

    printf -v "$var" "$pre%s$post$outsep" "${array[@]}"
  fi
}