Live data from Hacker News

What typing ^D does on Unix (2009)

utcc.utoronto.ca

61–70 of 162 posts

Re: What typing ^D does on Unix (2009)

#61
post #24

Pressing enter then ~. will gracefully shutdown hung ssh sessions

More generally, type ~ as an escape sequence. For example, type ~^Z to put a session in the background. Similarly, type ~~ to send an escape sequence in an inner session, etc...

...

  There's a program called SSH to get to another CPU.
  Tilde is the escape, it's doubled to send it through,
  And "quit" is tilde-.
I think you know how the rest goes...

Re: What typing ^D does on Unix (2009)

#62
post #53
post #40

Earlier quoted context omitted.

There's a lot of unexpected gems in the readline defaults. I recommend people try paging through "bind -p" in bash some time to see what's there. Personal favorites are C-r to search back in history and C-x C-e to edit the current command line in an external editor.

It is also very convenient to remap up/down keys to not just list history, but to do completion based on already typed characters: if [[ $- == *i* ]] then bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward' fi (fix: grammar)

Debian and Ubuntu typically have a commented-out mapping for this in /etc/inputrc as pgup/pgdn that I always enable in my ~/.inputrc. Editline supports something similar in .editrc IIRC.

Re: What typing ^D does on Unix (2009)

#63
post #28

Earlier quoted context omitted.

+++ATH0

How 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?

There 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.

Re: What typing ^D does on Unix (2009)

#64

As 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…

A super easy way to see this is to type cat, and then type asdf and hit ^D. It immediately echoes back what you typed. Another ^D quits cat, because it gets a 0-byte read that time.

Re: What typing ^D does on Unix (2009)

#65
post #14
post #9

Earlier quoted context omitted.

Those 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.

Actually the remote sshd does not need to be responsive, because " ~." is interpreted by the local ssh client. Very convenient if the TCP connection is stuck. You can also type " ~?" for a list of other commands supported by the local ssh client. Among other things, " ~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 co…

Ahg, I wish it let you scp files into your working directory too. That's be convenient.

Re: What typing ^D does on Unix (2009)

#66
post #54

^U will clear the buffer - super convenient for retrying botched passwords.

Fun trick: If somebody is watching you type in your login password or SSH pubkey password, spaz wildly on the keyboard, frantically typing random characters, and then hit ^U and type in your real password. Then you give them a puzzled look once you successfully log in as if nothing out of the ordinary just happened and say something like "What, don't you have a sufficiently complex password?".

I would like to recommend against spazing wildly at work.

Edit: It was just a joke :(

Re: What typing ^D does on Unix (2009)

#67
post #40
post #25

Earlier quoted context omitted.

In readline, ^U will only clear from the cursor to the start of the line, leaving anything on the right. ^K is the converse. I'm not sure you are speaking about readline though. Another very useful thing I use daily is Alt-F/B, to move forward/backward in the line one word at a time. Control does the same, one letter at a time. D will delete an element instead.

There's a lot of unexpected gems in the readline defaults. I recommend people try paging through "bind -p" in bash some time to see what's there. Personal favorites are C-r to search back in history and C-x C-e to edit the current command line in an external editor.

Emacs users will recognize many of the readline defaults -- unsurprisingly, given both are GNU software.

I use "yank-last-arg" most often, which is M-. by default.

  $ mkdir abcd
  $ cd 
On Zsh, I have M-, bound to "copy-earlier-word", which makes M-. M-, M-, cycle through the arguments to the previous command.

In reading the manpage for Bash, which doesn't have this function, I've just found "history-search-backward", which searches through history to find commands based on what you've already typed. That will come in useful!

  $ bind '"\er":history-search-backward'
  $ bind 
(Note that M-r is bound to "revert-line" by default, but I doubt I'll use that.)

These functions are all listed, with explanations, in the Bash and Zsh manpages.

Edit: As I see from another comment, a binding for "history-search-backward" to the Page Up key is in /etc/inputrc on Debian and derivatives.

Re: What typing ^D does on Unix (2009)

#69
post #36

Earlier quoted context omitted.

And sadly ^t doesn't do the Tenex function of a snapshot of sysstat for the current process.

It does on BSDs and OS X, but not on SYSV derivatives and Linux because NIH.

It generates SIGINFO on BSD. Which is even more useful and lets long running programs display progress of their choosing. E.g. how far has this big file copy come along?

On Linux, some programs (e.g. dd) actually do the same in SIGUSR1. I don't think it's possible to bind a key to sending USR1 so if you start a long dd you have to run the kill1 -USR from another shell. Or an awkward ^Z, then`kill -USR1 $(jobs -p)` then `fg`.

Post reply on HN