ahh

~$ which cat
  • Digit@lemmy.today
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 days ago

    I’m scared to run that.

    Is that like some kind of fork bomb?

    Or is it just a tubular cat?

    • AVincentInSpace@pawb.social
      link
      fedilink
      English
      arrow-up
      2
      ·
      6 hours ago

      it won’t… do anything

      it’ll wait for you to type ten lines (the default for head without arguments), buffer them (bc tail must buffer its input), then spit them back at you, and exit

      • head takes the first N lines of its input (if -n <number> is not specified, the default is 10 lines, and default reading from stdin, which in this case is your terminal) and prints them to its stdout (which thanks to the pipe, goes directly to cat’s stdin), then signals end-of-file.
      • cat accepts any number of filenames and conCATenates them (hence the name), writing their contents one after the other to stdout. with only one file as argument (the most common case in practice) it will simply write that file’s contents to stdout. with no arguments (as in this case) it will simply copy everything from its stdin (in this case head’s stdout) to its stdout (in this case tail’s stdin). the cat command could be completely omitted from this invocation (leaving simply head | tail) and it would work exactly the same.
      • tail reads its entire input (like head, it defaults to reading from stdin unless a filename is specified) and buffers it, then, when the end of the file is reached, it prints the last N lines is read (again defaulting to 10 unless -n <number> is specified) to stdout (in this case your terminal).

      put all together, head will wait for you to type a line, then will pass it on to cat, which will then immediately pass it on to tail, which will add it to a buffer and wait for another line. rinse and repeat until you have typed 10 lines, and then head will exit and the pipe between it and cat will close. cat, noticing the closed pipe, will exit in turn, causing the pipe between it and tail to close. tail, seeing an end of file signal on its input pipe, will cough up the last 10 lines in its buffer and then exit. since head only sent it 10 lines, this is the entirety of its input. so it will spit back everything you typed.

      If you press Ctrl+D to send head an end-of-file signal before you have typed 10 lines, head will exit immediately, causing cat to exit, causing tail to output everything it had in its buffer so far.