Is there similar Python zip() functionailty in bash? To be specific, I\'m looking for the equivilent functionality in bash without using python:
$ echo \"A\" >
Here's another way using the paste
command:
$ echo A > test_a
$ echo B >> test_a
$ echo X > test_b
$ echo Y >> test_b
$ paste test_a test_b | while read a b; do echo "$a, $b"; done
A, X
B, Y
You can do all sorts of fun stuff this way using process substitution, like
$ paste <(seq 1 3) <(seq 20 22) | while read a b ; do echo "$a = $b"; done
1 = 20
2 = 21
3 = 22
Be careful though, as when one of the arrays is longer, the variables can get mixed up:
$ paste <(seq 1 3) <(seq 20 25) | while read a b ; do echo "$a = $b"; done
1 = 20
2 = 21
3 = 22
23 =
24 =
25 =
This solution scales to any number of variables:
$ paste <(seq 1 3) \
$ <(seq 20 22) \
$ <(echo first; echo second; echo third) \
$ <(head -n3 /etc/passwd) | while read a b c d ; do echo "$a = $b = $c = $d"; done
1 = 20 = first = root:x:0:0:root:/root:/bin/bash
2 = 21 = second = daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3 = 22 = third = bin:x:2:2:bin:/bin:/usr/sbin/nologin