---
created_at: '2016-09-06T17:12:23.000Z'
title: What typing ^D does on Unix (2009)
url: https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects
author: luu
points: 536
story_text: 
comment_text: 
num_comments: 156
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1473181943
_tags:
- story
- author_luu
- story_12437527
objectID: '12437527'

---
[Source](https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects "Permalink to  Chris&#39;s Wiki :: blog/unix/TypingEOFEffects ")

#  Chris&#39;s Wiki :: blog/unix/TypingEOFEffects 

[Chris Siebenmann][1] :: [CSpace][2] » [blog][3] » [unix][4] » TypingEOFEffects

Welcome, guest.

## What typing ^D really does on Unix

September 6, 2009

Everyone knows what ^D does on Unix; it generates an end of file, so that `cat` or various other programs will exit (especially if they have [accidentally wound up reading from standard input][5]).

But this is just the usual effects of typing ^D; it is not what it actually does. What ^D actually does is simple but subtle:

> **Typing ^D causes the tty driver to immediately finish a `read()`.**

Normally doing a `read()` from a terminal is line-buffered inside the tty driver; your program only wakes up when the tty driver sees the newline, at which point you get back the full line. (Note that this buffering is distinct from anything that your language's IO system [may be doing][6].)

Typing ^D causes the tty driver to stop waiting for a newline and immediately return from the `read()` with however much of the line has been accumulated to date. If you haven't typed anything on the line yet, there is nothing accumulated and the `read()` will return 0 bytes, which is conveniently the signal for end of file. If you have typed something the program will get it; because it doesn't have a trailing newline, the program's own line-buffering may take over and keep `read()`ing to get the rest of the line.

(Other programs will immediately process the partial line with no buffering; `cat` is one example of this.)

Once you've typed ^D on a partial line, that portion of the line is immutable because it's already been given to the program. Most Unixes won't let you backspace over such partial lines; effectively they become output, not input.

(Note that modern shells are not good examples of this, because they don't do line-buffered input; to support command line editing, they switch terminal input into an uninterpreted mode. So they get the raw ^D and can do whatever they want with it, and they can let you edit as much of the pending line as they want.)

([One comment][7].)

Written on [06][8] [September][9] [2009][10].   

| ----- |
| 

|  «  |  [Why the Unix `EINTR` semantics (probably) exist][11]  | 

 | 

| ----- |
|  [A use for ticketing systems as your primary support method][12]  |  »  | 

 | 

These are my [WanderingThoughts][3]   
([About the blog][13])

[Full index of entries][14]   
[Recent comments][15]

This is part of [CSpace][16], and is written by [ChrisSiebenmann][17].   
Twitter: [@thatcks][18]

* * *

Categories: [links][19], [linux][20], [programming][21], [python][22], [snark][23], [solaris][24], [spam][25], [sysadmin][26], [tech][27], [unix][4], [web][28]

This is a [DWiki][29].   
[GettingAround][30]   
([Help][31])

Search: 

* * *

Page tools: [View Source][32], [Add Comment][33]. 

Search: 

Login:  Password: 

Atom Syndication: [Recent Comments][34].

* * *

Last modified: Sun Sep 6 01:10:49 2009   
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.

[1]: https://utcc.utoronto.ca/~cks/
[2]: https://utcc.utoronto.ca/~cks/space/
[3]: https://utcc.utoronto.ca/~cks/space/blog/
[4]: https://utcc.utoronto.ca/~cks/space/blog/unix/
[5]: https://utcc.utoronto.ca/~cks/space/blog/programming/AShellScriptGotcha
[6]: https://utcc.utoronto.ca/~cks/space/blog/python/FileIteratorProblems
[7]: https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects?showcomments#comments
[8]: https://utcc.utoronto.ca/~cks/space/blog/2009/09/06/
[9]: https://utcc.utoronto.ca/~cks/space/blog/2009/09/
[10]: https://utcc.utoronto.ca/~cks/space/blog/2009/
[11]: https://utcc.utoronto.ca/~cks/space/blog/unix/WhyEINTR
[12]: https://utcc.utoronto.ca/~cks/space/blog/sysadmin/UserTicketingUse
[13]: https://utcc.utoronto.ca/~cks/space/AboutBlog
[14]: https://utcc.utoronto.ca/~cks/space/blog/__Index
[15]: https://utcc.utoronto.ca/~cks/space/blog/__RecentComments
[16]: https://utcc.utoronto.ca/~cks/space/FrontPage
[17]: https://utcc.utoronto.ca/~cks/space/People/ChrisSiebenmann
[18]: https://twitter.com/thatcks/
[19]: https://utcc.utoronto.ca/~cks/space/blog/links/
[20]: https://utcc.utoronto.ca/~cks/space/blog/linux/
[21]: https://utcc.utoronto.ca/~cks/space/blog/programming/
[22]: https://utcc.utoronto.ca/~cks/space/blog/python/
[23]: https://utcc.utoronto.ca/~cks/space/blog/snark/
[24]: https://utcc.utoronto.ca/~cks/space/blog/solaris/
[25]: https://utcc.utoronto.ca/~cks/space/blog/spam/
[26]: https://utcc.utoronto.ca/~cks/space/blog/sysadmin/
[27]: https://utcc.utoronto.ca/~cks/space/blog/tech/
[28]: https://utcc.utoronto.ca/~cks/space/blog/web/
[29]: https://utcc.utoronto.ca/~cks/space/dwiki/DWiki
[30]: https://utcc.utoronto.ca/~cks/space/help/GettingAround
[31]: https://utcc.utoronto.ca/~cks/space/help/Help
[32]: https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects?source
[33]: https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects?writecomment
[34]: https://utcc.utoronto.ca/~cks/space/blog/unix/TypingEOFEffects?atomcomments

