Honestly I had no idea what ctrl+d even did, I just knew it was a convenient way for me to close all the REPL programs I use. The fact that it is similar to pressing enter really surprised me, so I wanted to share this knowledge with you :)

  • double_quack@lemm.ee
    link
    fedilink
    English
    arrow-up
    18
    arrow-down
    1
    ·
    edit-2
    2 days ago

    Ctl-D is the End-of-File character. Programs interpret it as “that’s it, the input you were reading has finished”, and react accordingly.

    • tuna@discuss.tchncs.deOP
      link
      fedilink
      arrow-up
      4
      arrow-down
      2
      ·
      2 days ago
      $ cat
      You sound very nice :)
      You sound very nice :)
      Bye<ctl-d>Bye
      
      Oh wait, and cool too
      Oh wait, and cool too
      <ctl-d>
      $ 
      

      The Ctl-D didn’t end the file when i typed “Bye” :( it only worked when I pressed Ctl-D on its own line. So how does cat know that it should ignore the EOF character if there is some text that comes before it?

      What Ctl-D does is flush the input to the program, and the program sees how big that input is. If the length of the input is 0 that is interpreted as EOF. So Ctl-D is like Enter because they both flush the input, but Ctl-D is unlike Enter because it does not append a newline before flushing, and as a consequence you can send empty input (aka an EOF “character”) with Ctl-D.

      • skepller@lemmy.world
        link
        fedilink
        arrow-up
        1
        arrow-down
        1
        ·
        edit-2
        2 days ago

        This!

        It’s merely a buffer flush, in case it’s empty, the program handling the input can choose how to interpret, cat decides to do it as an EOF.

        Reason why it also works as exit.