Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print two arrays side by side with bash script?

I couldn't find a good and simple answer to this question neither on google nor here on stackoverflow.

Basically I have two arrays that I need to print into the terminal side by side since one array is a list of terms and the other the terms's definitions. Does anyone know a good way of doing this?

Thanks in advance.

like image 652
Aslet Avatar asked May 12 '13 18:05

Aslet


1 Answers

Here's a "one-liner":

paste <(printf "%s\n" "${terms[@]}") <(printf "%s\n" "${defs[@]}")

This will create lines consisting of a term and a def separated by a tab, which might not, strictly speaking, be "side by side" (since they're not really in columns). If you knew how wide the first column should be, you could use something like:

paste -d' ' <(printf "%-12.12s\n" "${terms[@]}") <(printf "%s\n" "${defs[@]}")

which will pad or truncate the terms to 12 characters exactly, and then put a space between the two columns instead of a tab (-d' ').

like image 147
rici Avatar answered Nov 06 '22 06:11

rici