For those of you who want to get a solution using a function instead of checking directly, here is the solution I wrote. The latter works with regular arrays, associative arrays, and sparse arrays. But this requires at least Bash 4.3.
This function is part of the lib I wrote. As a symbol in my library, I use return values ββand the global retval argument for return statements that are more complex than a prime. The same applies to errorWithLog , which is just an echo on stderr and is logged as an error for the system logger, if the latter is available.
#-------------------------------------------------------------------------------
Examples of using:
declare -A table1=([hello]=world [ab]=cd) declare -A table2=([hello]=world [ab]=cd) if isArraysEqual table1[@] table2[@]; then echo "yes" else echo "no" fi
Gives yes .
declare -A table1=([hello]=world [ab]=cd) declare -A table2=([hello]=world [ab]=cde) if isArraysEqual table1 table2; then echo "yes" else echo "no" fi
Gives no .
declare -A table1=([hello]=world [abhe]=ce) declare -A table2=([hello]=world [ab he]=ce) if isArraysEqual table1 table2; then echo "yes" else echo "no" fi
Gives no .
table1=(1 2 3 4) table2=(1 2 "3 4") if isArraysEqual table1 table2; then echo "yes" else echo "no" fi
Gives no .
source share