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

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


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' ').


You can use a C-style for loop to accomplish this, assuming both arrays are the same length:

for ((i=0; i<=${#arr1[@]}; i++)); do    printf '%s %s\n' "${arr1[i]}" "${arr2[i]}"done