Search code examples
linuxpipehead

Understanding Pipes in linux


This may be a very simple question, but I don't understand what exactly is going on here, although I understand the commands yes, nl, and head individually.

yes | nl | head -1000 > data1.txt     

I don't understand how the pipe is interacting through all of these to make a data file with numbers 1-1000 on different lines with y next to each:

 1  y
 2  y
 3  y
 4  y
 5  y
 6  y
 7  y
 8  y
 9  y
10  y
11  y
12  y
13  y
14  y
15  y
16  y
17  y
18  y
19  y
20  y
21  y
22  y
23  y
24  y

etc.. up to 1000

Any explanation is appreciated.


Solution

  • The output of the left command will be passed as the input of the command on the right of the |.

    For you example, yes output unlimited number of y, and nl added rownumber to those y. Then the head command return the first 1000 lines of them.

    The > is not part of a pipe. It's used for redirecting your output from STDOUT to a file.