Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created Mar 21, 2012
Embed
What would you like to do?
Reverse
reverse() {
set -- "$@" "${2}[*]"
local -a 'keys=("${!'"$1"'[@]}")'
local -a "$2"=$'([${keys[n++]}]\'="${'"$1"$'[keys[\'{'$((${#keys[@]}-1))$'..0}\']]}"\')' # Generate elements
local -a "$2"=$'(\''"${!3}"$'\')' #Concatenate elements
local -a "$2"'=('"${!3}"')' #Build array
declare -p "$1" "$2";
}
fromArr=(4 [2]=3 5 6 [11]='9 10' 14 [15]=$'15[ -6');
reverse fromArr toArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment