`cat`を終了するにはなぜ^ Dを2回押すべきですか? [コピー]

`cat`を終了するにはなぜ^ Dを2回押すべきですか? [コピー]

実行しcatて入力aしてみましょう。終了しない^Dことがわかります。cat

cat+++と比較してみてくださいa。これで猫が終了します。Enter^D

それでは、なぜ最初のケースでは2回押す必要がありますが、^D2番目のケースでは1回だけ押す必要がありますか?cat^D

ベストアンサー1

答えはtermios(3)マニュアルページにあります:

   VEOF   (004, EOT, Ctrl-D) End-of-file character (EOF).  More precisely:
          this character causes the pending tty buffer to be sent  to  the
          waiting  user program without waiting for end-of-line.  If it is
          the first character of the line, the read(2) in the user program
          returns  0, which signifies end-of-file.  Recognized when ICANON
          is set, and then not passed as input.

最初のキーは^D入力した行をに送信するため、結果がcat得られます(1文字、EOL文字なし)。 2番目の結果は0を返し、これはEOFに達したことを示します。read(2)a^Dread(2)cat

おすすめ記事