Looping over pairs of values in bash

前端 未结 7 2041
萌比男神i
萌比男神i 2020-11-22 03:19

I have 10 text files and I want to paste each file with its pair, such that I have 5 total files.

I tried the following:

for i in 4_1 5_         


        
7条回答
  •  醉话见心
    2020-11-22 03:32

    There is difference between using read -r x y and read x && read y

    while read -r x y; do
        echo "$x and $y"
    done <<'____HERE'
        A B
        C D
    ____HERE
    

    will print

    A and B
    C and D
    

    whereas,

    while read x && read y; do
        echo "$x and $y"
    done <<'____HERE'
        A B
        C D
    ____HERE
    

    will print

    A B and C D
    

    First example splits on space, while second splits on newline.

提交回复
热议问题