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!
To access elements of array using index in Bash, use index notation on the array variable as array[index].
sort <(for f in "${files[@]}" ; do echo "$f" ; done)
Yet another solution:
printf "%s\n" "${files[@]}" | sort
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With