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.
When I'm using
migrated from serverfault.com Jan 21 '12 at 21:07
This question came from our site for system and network administrators.
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.
This method is just fine, really.
To abort a long command while typing, I sometimes jump to the start of line, and insert a comment sign, before hitting enter:
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.
In Bash, you can use the shortcut
to remarkify your command, as pointed out in the comments (Thanks, @Zorawar).
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.
If using Bash, one can also use Ctrl-Z, and type
bg to "put the current job in background".
As @Patrick said in the comment on the accepted answer,
Ctrl+Dfor programs that are reading input from the user.
Ctrl+Dcloses STDIN to the program.
This helped me when
Ctrl+C did not work to get the command line back after using