Bash – How does Bash pipe large amounts of data

bashlesspipe

Let's say you want to cat the contents of a really big file, but want to view it a few bits at a time. Let's say one were to do the following:

$ cat /dev/sda1 | less

As a programmer of languages such as Java and ActionScript, when I look at that code I imagine Bash first running the command cat /dev/sda1 (loading everything the command returns into RAM), and then running the command less which has access to that really big "pseudo-variable" represented as -.

Is that the way Bash does things (meaning that command is a really bad idea if the file is larger than the amount of RAM on your system, and you should use another command), or does it have a way of optimising the piping of large amounts of data?

Best Answer

No it doesn't load everything into memory, that would be an impractical way to design this. It uses buffers to buffer the output from the left side of the pipe, and then connect these buffers to the input of the command on the right side of the pipe.

The man page man 7 pipe has all the details, as well as this other U&L Q&A titled: How big is the pipe buffer?

Related Question