Search code examples
linuxbashunixpipe

Why does this shell pipeline exit?


I have a shell pipeline for generating 10 characters password at random:

cat /dev/urandom | base64 | head -c 10

My question is cat /dev/urandom | base64 is an infinite output stream which will not exit by itself. But why does appending head -c 10 make the whole pipeline exit? I assume cat, base64 and head are 3 separated processes, how can head make the cat process exit?


Solution

  • After base64 outputs 10 bytes, head gets enough inputs and exits. When the former attempts to output more bytes, it will receive SIGPIPE signal and hence exits too。For the same reason, cat will exit in turn.