13 ms·
What typing ^D does on Unix (2009)
- felixgallo 10y agoTyping +++ will bring you back out to your modem's firmware, at which point you can issue further AT commands
- smlacy 10y ago+++ATH0
- deleted 10y ago[deleted]
- nitrogen 10y agoHow did modems deal with pluses in data? Was there a way to escape the escape sequence, or was there just an ATSnnn or similar sequence to disable escapes?
- felixgallo 10y agoThere were a bunch of different mechanisms depending on vendor. Some required a specific time interval in between the + signs, so that typing very slowly, or instantly, wouldn't trigger the escape. Some vendors had bugs in which when +++ appeared as part of the incoming data it would also trigger the firmware escape. And of course programmable terminals had hilarious exploits where you'd get someone to run your program, it would program the terminal to emit a macro when you hit enter, the macro would include +++ATZ, etc., etc. My favorite modem story was when I worked for a company that shipped auto parts warehouse management systems. They would have several SCO unix boxes with serial port concentrator cards and port concentrators on those concentrators, such that you'd have 256 modems on a single box and RS-232 cables everywhere. Frequently the operators at the auto parts warehouses couldn't be bothered to do cable management, so at one facility, the entire floor of the server closet was carpeted literally 2 feet thick with a random mess of RS-232 cables, each one leading to a modem. When standing on the cables killed a modem or two, they would just string a new wire rather than try to fish out the old one. Kind of like Google's "let it sit in the rack" policy for failed servers, but writ extremely small and pathologically badly.
- somedangedname 10y ago> "let it sit in the rack" So do they do scheduled collections of failed units? Or just let them sit there indefinitely?
- pixl97 10y agoThe entire rack gets replaced with new equipment eventually. Probably get replaced every few years or so. For most of the SMB clients I have, I don't build new servers any longer. I can buy rackmount servers pulled out of a data center that are a few years old at about 1/3 the price. I just put new disks in them and a new OS.
- PhantomGremlin 10y agoHow did modems deal with pluses in data? Many years ago, modem maker Hayes Microcomputer Products held a patent dealing with that issue. The clever bit was in looking for <pause> +++ <pause> to effect the escape. Hayes tried to collect royalties, but this caused quite a kerfuffle. Cheap modem makers didn't bother looking for pauses. "Hilarity" ensued. Other solutions were found. https://en.wikipedia.org/wiki/Time_Independent_Escape_Sequence https://en.wikipedia.org/wiki/Time_Independent_Escape_Sequen...
- voidz 10y agoDarnit, I had to dial in again. Kidding of course, but once upon a time many 56k6 modem contained a bug that, upon receiving this in plain text, would cause an actual modem hangup. You could kill hundreds of IRC connections with that one.
- caf 10y agoIt wasn't exactly an unintentional bug - rather one company owned a patent on the technique of enforcing a guard time before and after the attention string to go into command mode (the Hayes '302 patent). They licensed it for $1/modem, but some manufacturers felt that they couldn't afford that.
- voidz 10y agoHa, I never knew! Thanks for adding this; who would have thought my 20 year old anekdote would get a refreshment. :-)
- gtrubetskoy 10y agoOK
- teh_klev 10y agoThank you for this blast from the past :)
- telotortium 10y agoAs a corollary, typing ^D twice on a non-empty line will also send EOF on the input, allowing you to provide a program with input not ending with \n from the command line. I knew about this behavior, but not why it worked, but this article's explanation clarifies this behavior as well. The first ^D terminates the read() call, passing the line so far, without terminating \n, to the program. The second causes read() to return with 0 read bytes, which is EOF, as explained in the article.
- mVChr 10y agoI know this article says Unix specifically, but on Linux (Ubuntu) I just tried typing `ls^D` in bash and got a terminal beep. I tried in zsh and got an autocomplete as if I'd hit tab. In ksh and fish nothing happened. I'm curious as to why there are different behaviors here?
- eridius 10y agoAs the article explained at the end, shells put the terminal into uninterpreted mode, so ^D doesn't do anything special there. You have to be using a program (such as cat) that doesn't do this to see ^D's behavior.
- telotortium 10y agoShells that want to implement features like tab completion or more elaborate line editing than the basics, which only provide for deleting the last character, word, or line (using Backspace/^W/^U) need to put the terminal into raw mode when reading the user's command line so they can bypass the basic line editing that the terminal provides (called "cooked mode"). However, you can write a shell that doesn't do this. In particular, on Linux, you can experiment in dash. You'll find that the behavior is exactly the same as this article, since the terminal remains in cooked mode. In particular, if you type `l^Ds`, that will run `ls`, though you won't be able to delete the `l` anymore since that was input before the `^D`. Also, if you type `ls^D^D`, that will log out just like typing `^D` on an empty line does. Other shells have different behaviors -- for example, Bash seems to take `ls^D` to be the same as typing `ls` followed by pressing Enter. Before any shell starts executing a program, however, it puts the terminal back in cooked mode no matter what mode it's in when at the prompt, since that's the default terminal mode expected by programs. If you want to experiment with the cooked mode line editing commands to get the behavior described in the article, you should run a command that receives standard input from the terminal (running `cat` by itself should suffice).
- djsumdog 10y agohuh. That's really interesting. I assume ^L is handled in the tty driver as well?
- kazinator 10y ago^L usually has no assigned action. $ stty -a | grep -F '^D' intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; $ stty -a | grep -F '^L' $ Your Ctrl-L clear-and-refresh command is implemented in Bash (or rather GNU readline). There is a "reprint" action, commonly bound to ^R: $ stty -a | grep rprnt eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; It doesn't clear the screen; it just issues a newline and reprints the characters buffered so far. You can use stty to assign ^L to "rprnt".
- ChuckMcM 10y agoAnd sadly ^t doesn't do the Tenex function of a snapshot of sysstat for the current process.
- kazinator 10y agoOn Linux, you can compile in kernel support for a "Magic SysRq" key which causes SysRq to give you a menu of various debug printing and other actions. It works on serial consoles also, where instead of SysRq, a serial line break signal can map to the action (so it is completely out-of-band relative to serial data). https://en.wikipedia.org/wiki/Magic_SysRq_key https://en.wikipedia.org/wiki/Magic_SysRq_key
- geococcyxc 10y agoNot sure what sysstat on Tenex did, but you can still use ^T on the BSDs and Mac OS X to get the wait channel and some additional stats.
- kps 10y agoIt does on BSDs and OS X, but not on SYSV derivatives and Linux because NIH.
- gtrubetskoy 10y ago^U will clear the buffer - super convenient for retrying botched passwords.
- harry8 10y ago^Y will paste the cleared buffer back again
- SilasX 10y agoSo ^U will let a partial password linger in memory longer than it needs to if you use it as suggested by the GP?
- yebyen 10y agoNo, I don't think so. Not unless you are accustomed to typing your password directly into the terminal prompt. Kill buffers don't pass from one application to another and they don't all behave the same in every context, either. (For example, if I type some stuff and then ^U to kill the password buffer at an SSH password prompt, typing ^Y after that seems to put the whole process in the background. Not sure what that's intended to do, but subsequent ^Y presses don't retrieve the password into the shell prompt...)
- JdeBP 10y agoIn canonical mode, EM is the usual "delayed suspend" special character.
- klodolph 10y agoTo elaborate on yebyen's answer: these shortcuts can be handled in different layers of your stack. By default, ^U will be handled by your terminal emulator, and it will only delete the buffer, it won't remember what you've deleted. However, programs can ask the terminal emulator to pass ^U through to them so they can handle it themselves. This happens if you use libeditline (or libreadline), and these libraries implement a per-application buffer. You can test this yourself. Type some text, then ^U, then ^Y. This works because your shell uses libreadline or libeditline. Try this again in a "dumb" program like cat, or a shell without line editing like /bin/dash. ^U works, but ^Y does nothing (well, it lets you type ^Y in as much as you want).
- SteveNuts 10y agoPressing enter then ~. will gracefully shutdown hung ssh sessions
- alfanick 10y agonice one, never seen that - always used ^D/exit
- wyldfire 10y agoThose are yet more graceful -- for when the shell is responsive. Use ~. in cases where for some reason you cannot get access to the terminal anymore but remote sshd is responsive.
- abstractbeliefs 10y agoiirc, enter-~-. isn't anything to do with the remote sshd. Instead, it escapes to the local ssh client (enter-~) that it should close (.). Which is really useful for if the remote sshd blew up, or, more commonly, your connection was broken.
- aji 10y ago~. (and the other ~ commands) are handled locally, not by the remote sshd. I use it when my network cuts out or something and my ssh session is unrecoverably stuck
- 0x0 10y agoActually the remote sshd does not need to be responsive, because "<cr>~." is interpreted by the local ssh client. Very convenient if the TCP connection is stuck. You can also type "<cr>~?" for a list of other commands supported by the local ssh client. Among other things, "<cr>~C" opens a command line interpreter on the client side of the ssh client, allowing you to add and remove TCP port forwarding without restarting the connection.
- Tyr42 10y ago
- TorKlingberg 10y agoI wish articles like this would say what ^D means. I remember when I was new to Unix it took me a while to realize that I wasn't supposed to type a literal caret (^) followed by a D. For reference it means Ctrl+D, unless you you have a strange keyboard from Sun or something.
- zeveb 10y ago^D, Ctrl+D and C-d are all different ways of saying the same thing. There's nothing particularly neutral about Ctrl+D, any more than there is about using the word 'water' to mean the substance of which the oceans are mostly comprised — it's just another dialect's term.
- Rangi42 10y agoBut Ctrl is easier to figure out from context, since it's a multi-character Googlable symbol that's even printed on the key.
- jrlocke 10y agoIt's not about neutrality, it's about ease of understanding. For me at least, when speaking about something that can be entered into a keyboard ^D is less obvious than Ctrl+D–I need to press the key labeled "control" to issue the proper command, the key labeled "^" will not suffice.
- joosters 10y agoTo be really pedantic (sorry! I'm not complaining about your comment), your keyboard does not have any key labelled 'control', it is always (AFAIK) abbreviated as 'ctrl'. Which is really weird to me, as it's hardly a big word needing abbreviating, and keyboards often have other long words written on the keys ('delete', 'page down', and so on). On top of that, the shortened 'ctrl' is really a terrible abbreviation. It's not obvious that it stands for 'control' and I would guess that most non-techy people would have no idea that it means this word. Plus, it's near to another key with multiple letters ('alt'). The overall effect is that 'ctrl' appears to just be a jumble of letters with some esoteric meaning. A really odd design decision. Why not just put 'control' on the key and be done with it?
- wruza 10y agoIn general, CTRL-anything send corresponding control character by shifting keys to control set of ASCII table [1] (shift-key shifts keys to upper set). Many of them retained original meaning, e.g. ^I ^M ^J ^H ^D ^[. [1] http://ascii-table.com/ http://ascii-table.com/
- voidz 10y agoExpanding on this, ^\ (Ctrl+\) means sigkill, and I don't think many people know about this one.
- giodamelio 10y agoWow, I can't believe I didn't know that. No more running killall in a new terminal!
- verbatim 10y ago^\ is actually sigquit, which is a bit different from sigkill, since programs can catch QUIT and perform some cleanup.
- semi-extrinsic 10y agoAlso ^| (as in, "control pipe") which is a synonym for ^\. At least for me, | is faster to hit than \ And since we're on this topic: if I write a for loop in bash that runs a slow command 1000 times (ImageMagick comes to mind), and I realise something has gone wrong, is there an easy way of breaking the outer loop?
- dragonwriter 10y ago> Also ^| (as in, "control pipe") which is a synonym for ^\. At least for me, | is faster to hit than \ How? Do you have a keyboard where | isn't Shift+\?
- Nullabillity 10y agoOn a Swedish keyboard, | is altgr+< (between shift and z), and \ is altgr++ (between 0 and ´).
- semi-extrinsic 10y agoI have a Norwegian keyboard, where as you can see, pipe has its own key to the left of "1" (above Tab): https://en.m.wikipedia.org/wiki/File:Keyboard_Layout_Norwegian.png https://en.m.wikipedia.org/wiki/File:Keyboard_Layout_Norwegi...
- VintageCool 10y agoNeat. So when my terminal is spewing output because I ran "cat massivefile" instead of "cat massivefile | head", I can hit ctrl-D to stop it instead of repeatedly hitting ctrl-C and despairing.
- gonvaled 10y agoI don't think so, since cat is reading from massivefile, not from your tty.
- rfw1z 10y agoNo, thats what ctrl-s and ctrl-q are for. Here's the rest in one place: eof VEOF EOF character eol VEOL EOL character eol2 VEOL2 EOL2 character erase VERASE ERASE character erase2 VERASE2 ERASE2 character werase VWERASE WERASE character intr VINTR INTR character kill VKILL KILL character quit VQUIT QUIT character susp VSUSP SUSP character start VSTART START character stop VSTOP STOP character dsusp VDSUSP DSUSP character lnext VLNEXT LNEXT character reprint VREPRINT REPRINT character status VSTATUS STATUS character cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>; eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;
- yread 10y agoSo many useful (and utterly undiscoverable) tidbits of knowledge in this thread! Thanks!
- Nilzor 10y agoThis comment thread is worth much more than the actual article
- Nilzor 10y agoThis comment thread is worth much more than the actual article
- Nilzor 10y agoThis comment thread is worth much more than the actual article
- coretx 10y agoDirectly after reading the title the question "what does ^D do" was fired at a Old Unix Beard friend of Mine who happens to be here for coffee. Instant reply Without any latency: "It originally was meant to terminate the Tape drive"
- peterwwillis 10y agoThe author actually clouds what and why is really happening a bit. Ctrl+D sends the EOF character to the terminal driver. The terminal driver then determines, based on its settings, what to do next. In the 'icanon' mode, the terminal driver implements a rustic line editor, so you can do things like Backspace to delete previous characters and so on. Pressing Enter during this mode returns the edited buffer line to the program. Where Ctrl+D, or EOF, comes in is when you want to return a line to the program _without_ pressing Enter. This is where the terminal driver returns the buffer to the read() function for the program immediately. Doing it again with no additional input shows that you were done line-editing, so it simply returns nothing, signaling the program that you're done editing or providing input - the intended purpose of End Of File, the character sent by Ctrl+D. (If you were reading a file and you received nothing on a read... you would be at the end of a file, because why else would the file have nothing else to read?) But this 'icanon' mode won't be active in your actual terminal shell, because shells have their own line editing implementation, so they turn 'icanon' off by default. You can, of course, turn on or off in the terminal with "stty". Use "stty -a" to see all the other current settings. The terminal application, by the way, sets the EOF to the same value as the terminal driver's by default, in order to prevent confusion.
- userbinator 10y agoNo TTY-related discussion is complete without a reference to Linus Akesson's extremely lucid article on the whole thing, including what all the other interesting Ctrl+ characters do: http://www.linusakesson.net/programming/tty/ http://www.linusakesson.net/programming/tty/ ...and the extensive previous HN discussion on it: https://news.ycombinator.com/item?id=10631513 https://news.ycombinator.com/item?id=10631513 https://news.ycombinator.com/item?id=10064657 https://news.ycombinator.com/item?id=10064657 https://news.ycombinator.com/item?id=8094186 https://news.ycombinator.com/item?id=8094186 https://news.ycombinator.com/item?id=4544318 https://news.ycombinator.com/item?id=4544318 https://news.ycombinator.com/item?id=4062981 https://news.ycombinator.com/item?id=4062981 https://news.ycombinator.com/item?id=658155 https://news.ycombinator.com/item?id=658155
- happyslobro 10y ago6 KB HTML. 4 KB CSS with comments preserved. No JS. It's so beautiful. I think I'm going to cry :)
- pantulis 10y agoIf memory serves well, there was also the amazing Ctrl-O, which allowed you to send control sequences over a busy terminal --for example when scrolling a lot of text, seems to be mapped to the arcane VDISCARD and unsupported by Posix & Linux.