1

If I have this array

arra[0]='Debian'
arra[1]='Debian'
arra[2]='Ubuntu'
arra[3]='Suse'

How can I stock in a new array Debian, Ubuntu and Suse only ? like this

new[0]='Debian'
new[1]='Ubuntu'
new[2]='Suse'
1

A working answer:

eval newArray=($(for i in  "${arra[@]}" ; do  echo "\"$i\"" ; done | sort -u))

It works with spaces in your array elements like in 'Red Hat': you need to print quote "" around your variable value ("\"$i\"") and eval the result.

A more elegant alternative

eval newArray=($(printf "%q\n" "${arra[@]}" | sort -u))

where the format %q is needed to quote the output of printf.

  • Thus works but eval in shell scripting is almost always a bad idea. Better to upgrade to bash 4 and get true associative arrays. – Nifle Feb 10 '15 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.