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