How can I use a Bash array as input to a command?

Say, for example, I have the following array:

files=( "foo" "bar" "baz fizzle" )

I want to pipe the contents of this array through a command, say sort, as though each element where a line in a file. Sure, I could write the array to a temporary file, then use the temporary file as input to sort, but I'd like to avoid using a temporary file if possible.

If "bar fizzle" didn't have that space character, I could do something like this:

echo ${files[@]} | tr ' ' '\012' | sort

Any ideas? Thanks!

6
задан splicer 31 March 2011 в 18:15
поделиться