Skip to content

Instantly share code, notes, and snippets.

@suewonjp
Created February 6, 2017 12:37
Show Gist options
  • Save suewonjp/7150f3fe449a58b2ce6cbb456882bed6 to your computer and use it in GitHub Desktop.
Save suewonjp/7150f3fe449a58b2ce6cbb456882bed6 to your computer and use it in GitHub Desktop.
#!/bin/bash
sort_array() {
if [ -n "$1" ]; then
local IFS=$'\n'
eval "local arr=( \${$1[*]} )"
#arr=( $( printf "%s\n" ${arr[@]} | sort ) )
arr=( $( sort <<<"${arr[*]}" ) ) ### <<< is here string notation
eval "$1=( \${arr[*]} )"
fi
}
@suewonjp
Copy link
Author

suewonjp commented Feb 6, 2017

Usage
tmp=( c d a e b )

sort_array tmp

echo ${tmp[*]} ### a b c d e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment