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?
cat /dev/sda1 | less
, bash creates the pipe between the two processes, but after that it is implemented by the OS kernel. Thecat
andtail
commands run in parallel; see In what order do piped commands run? And, in case this isn’t clear from Karthik’s answer: if thecat
gets ahead of thetail
(i.e., thecat
writes to the pipe faster than thetail
reads from it), the operating system will force thecat
to pause until thetail
catches up with it. – Scott - Слава Україні Oct 31 '14 at 19:42