What's the difference between ^C and ^D for UNIX/Mac OS X terminal?

69

37

When I try to stop something, I use ^C and sometimes ^D in terminal.

What's the difference between the two? Why some program doesn't respond to ^C, but ^D. Why the Terminal.app just quits when I use ^D?

prosseek

Posted 2010-07-28T21:02:37.527

Reputation: 4 635

FYI: See Wikipedia pages for: END OF TEXT character U+0003 produced by Control+C, and END OF TRANSMISSION character U+0004 produced by Control+D.

– Basil Bourque – 2019-03-04T04:16:26.190

Answers

69

CtrlC tells the terminal to send a SIGINT to the current foreground process, which by default translates into terminating the application. CtrlD tells the terminal that it should register a EOF on standard input, which bash interprets as a desire to exit.

Ignacio Vazquez-Abrams

Posted 2010-07-28T21:02:37.527

Reputation: 100 516

73

Ctrl+D (^D) means end of file. It only works at the beginning of a line (I'm simplifying a little), and has no effect if the program isn't reading input from the terminal. In your experiment, ^D told the shell that you weren't going to type any more commands, so it exited; then the terminal exited because its subprogram had terminated.

Ctrl+C (^C) means “interrupt”, i.e., stop what you're doing. Technically, pressing ^C sends the INT signal, which by default terminates an application, but which in many programs means go back to the top level (e.g., in a shell, stop typing a command line and go back to a pristine prompt).

If a program doesn't respond to ^C, you can try Ctrl+\ (^\). This sends the QUIT signal, which by default terminates an application, and which not so many programs intercept.

Another key that sends a signal is Ctrl+Z (^Z). It sends the TSTP signal, which pauses the program running in the foreground. (TSTP is short for “terminal stop”; it's similar to STOP but TSTP can be ignored whereas STOP cannot.) From the shell, you can resume that program's execution with the fg command (resume in the foreground) or the bg command (resume in the background).

All of these keys can be changed with the stty command. Some programs, particularly full-screen programs that have key bindings, disable them.

Gilles 'SO- stop being evil'

Posted 2010-07-28T21:02:37.527

Reputation: 58 319

Needed this! it was stuck, and i couldn't get out – ahnbizcad – 2015-05-15T08:19:00.230

1Just a suggestion regarding CTRL-D: it means "flush the input I typed so far" and If the line is empty, then read() returns with zero, and that is interpreted as "end of file". – luca – 2017-04-05T14:49:54.210

This is super important and helpful information; thanks for the comprehensive and clear explanation – iono – 2018-09-26T07:12:09.073

Just being curious, why does the python progam that run in shell doesn't quit with ctrl + c, instead it prompts KeyboardInterrupt? It does quit with ctrl + d (if at the beginning of a line), and with ctrl + . – Rick – 2019-03-28T11:39:45.423

@Rick Ctrl+C in the interactive python interpreter brings you back to Python's command reader. That's what it's supposed to do. – Gilles 'SO- stop being evil' – 2019-03-28T12:58:18.160

A small correction: Control-Z sends the TSTP (terminal stop) signal. This can be caught or ignored by processes, whereas the STOP signal cannot be. – Chris Page – 2011-09-19T11:53:38.583

3

Adding on to the 2 really good answers above, here's an example:

If you type python in the shell, it takes you into python's >>> prompt.

Now, if you hit Ctrl+C, it'll say KeyboardInterrupt and stay in the >>>.
If you enter into a for loop, e.g. by typing for x in (text): it waits for you to type further by showing a ... prompt, if you hit Ctrl+C now, it'll exit from the for statement and return to the >>> prompt
If you hit Ctrl+D at any point, whether in >>> or ... it will exit out of the python prompt and return to the original shell.

Similarly, if ssh'ed into another machine, a Ctrl+C will terminate any existing commands, Ctrl+D will do that and exit from the machine as well. (Also, the Delete key is same as doing a Ctrl+D)

Siddhartha

Posted 2010-07-28T21:02:37.527

Reputation: 527

I appreciate this answer. However, I found some inaccuracies when trying it out for myself. While ssh'ed into a remote machine and running the python process, Ctrl+D exited python and took me back to the remote machine's prompt, it did not exit from the machine. Also, the Delete key did not work the same as Ctrl+D in any case, running python, at the remote prompt, or at my local prompt. It didn't seem to do anything, except actually delete characters while typing in python, of course. However, Ctrl+D did act as the Delete key when typing, it deleted characters. – Stack Underflow – 2018-04-20T17:13:57.283

1That's interesting, I do think it might depend on the kind of shell you're using (bash, ksh etc). – Siddhartha – 2018-04-21T21:04:11.777