Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created March 21, 2012 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/2147018 to your computer and use it in GitHub Desktop.
Save ormaaj/2147018 to your computer and use it in GitHub Desktop.
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