Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 16 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 Sep 19 '11 at 11:53
add comment

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)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.