Remix.run Logo
Grimeton 3 days ago

There are no objects in bash. There are indexed and associative arrays and both can be iterated over like so:

  for value in "${SOMEARRAY[@]}"; do
    echo "${value}"
  done
or with the help of the keys:

  for key in "${!SOMEARRAY[@]}"; do
    echo "key: ${key} - value: ${SOMEARRAY["${key}"]}"
  done
If you want to dump the data of any variable you can just use declare -p

  declare -p SOMEARRAY
and you get something like this:

  declare -a SOMEARRAY=([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f")
What you can do, if you have a set of variables and you want them to be "dumped", is this:

Let's "dump" all variables that start with "BASH":

  for k in "${!BASH@}"; do
    declare -p "${k}"
  done
Or one could do something like this:

  for k in "${!BASH@}"; do
    echo "${k}: ${!k}"
  done

But the declare option is much more reliable as you don't have to test for the variable's type.
westurner 3 days ago | parent [-]

Bash has associative arrays, but just POSIX shells like ash do not have associative arrays.

And, POSIX shell can only shift and unshift on the $@ array; so it would be necessary to implement hashmaps or associative arrays with shell string methods and/or eval.