Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I'm using tail -f and I want to return to the shell, I always use CTRL+C. Or when I am typing a command and feel like aborting it and starting over, I simply CTRL+C to get back to an empty command line prompt. Is this considered bad practice? I sometimes feel there might be a better way to break away from something, but really have no idea.

share|improve this question

migrated from serverfault.com Jan 21 '12 at 21:07

This question came from our site for professional system and network administrators.

5  
To cancel a partially entered shell command, you can use Control-U or Delete (depending on your tty settings and key bindings), but there's nothing wrong with using Control-C. –  Keith Thompson Jan 21 '12 at 20:56
    
@KeithThompson thanks for the Ctrl+U, I wasnt aware of that one :-) –  Patrick Jan 21 '12 at 22:47
    
For the record, C-u doesn't actually cancel the command entered, it moves it to the paste buffer (similar to the "cut" functionality of some programs). –  Chris Down Nov 9 '12 at 13:43

5 Answers 5

up vote 33 down vote accepted

Ctrl+C sends a SIGINT to the program. This tells the program that you want to interrupt (and end) it's process. Most programs correctly catch this and cleanly exit. So, yes, this is a "correct" way to end most programs.

There are other keyboard shortcuts for sending other signals to programs, but this is the most common.

share|improve this answer
7  
There's also Ctrl+D for programs that are reading input from the user. Ctrl+D closes STDIN to the program. For gracefully exiting the shell this is also available (exact same as typing exit). –  Patrick Jan 21 '12 at 22:48
1  
@Patrick Ctrl+D actually sends an End of File indicator to the stream. It's up to the program to respond appropriately (usually closing the file stream). –  Chris S Jan 22 '12 at 2:32
    
thats what closing STDIN does. You cant get EOF without the pipe/stream being closed. –  Patrick Jan 22 '12 at 3:27
8  
Wrong on three counts, Patrick. First: Control+D doesn't have this meaning for pipes. Second: the EOF special character doesn't close anything. It causes a one time return of zero bytes (presuming that the line buffer is empty) from the current read() system call. The next read()s are unaffected. Third: None of this applies to programs which put the line discipline into non-canonical mode, such as most shell programs from the past couple of decades and indeed any other program that uses a library such as GNU readline. –  JdeBP Jan 22 '12 at 11:32

This method is just fine, really.

share|improve this answer

To abort a long command while typing, I sometimes jump to the start of line, and insert a comment sign, before hitting enter:

Home#Enter

Pos1#Return

This is useful, if I typed a copy command with a long path, for example, and meanwhile observe, that I first need to create the directory, but would like to repeat the command afterwards. Then I just have to take it from the history, delete the hash, and enter it.

share|improve this answer
5  
You can also use Ctrl-A without taking your fingers off the home row. –  Dietrich Epp Jan 22 '12 at 3:30
1  
And Ctrl-E goes to the end, by the way. –  Dave Jan 27 '12 at 18:15
    
@Dave: That's right, but you don't need to move to the end to hit enter, to confirm your command. The commandline is not an editor, where this might insert a newline. –  user unknown Jan 27 '12 at 20:12

Generally speaking, using Ctrl+C is fine when the program offers you no interactive way to terminate (either by design, or, more frequently, because it has frozen or become unusable). Just bear in mind that, when in interactive mode, the key combo you really want may be Ctrl+D, which sends an EOF, signaling the end of the input.

share|improve this answer

If using Bash, one can also use Ctrl-Z, and type bg to "put the current job in background".

share|improve this answer

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.