Print a bash array by name
This uses eval
, as that is the only way to reference an array
that has its name stored in a variable while retaining indices (for
associative and sparse arrays).
More info on why indirection won't work for this here
http://mywiki.wooledge.org/BashFAQ/006#Evaluating_indirect.2Freference_variables
Note: the output generated by this function is NOT safe to eval
by the shell, this is just meant to inspect the contents of array,
like var_dump
in PHP, or console.dir
in JavaScript
$ a=([0]="hello" [1]="how are" [5]="you")
$ parr a
(
[0]=`hello`
[1]=`how are`
[5]=`you`
)
This will also work for associative arrays in Bash >= 4
$ parr BASH_ALIASES
(
[urldecode]=`python -c 'import sys;import urllib as u;print u.unquote_plus(sys.stdin.read());'`
[basher]=`~/.basher/basher`
[joyentstillpaying]=`sdc-listmachines | json -a -c "state !== 'running'" name state`
[gerp]=`grep`
[lsdisks]=`kstat -lc disk :::class | field 3 :`
[ls]=`ls -p --color=auto`
[cdir]=`cd "${_%/*}"`
[urlencode]=`python -c 'import sys;import urllib as u;print u.quote_plus(sys.stdin.read());'`
[cpp2c]=`sed -e 's#//\(.*\)#/*\1 */#'`
[externalip]=`curl -s http://ifconfig.me/ip`
[l]=`ls -CF`
[lsnpm]=`npm ls -g --depth=0`
[chomd]=`chmod`
)
note: this is taken from https://github.com/bahamas10/bash-interactivity