Shell – How to exit or cancel a bad bash command

command linekillshellterminal

I expect to get some flak for this, but I can't find the answer anywhere. It seems like it should be so obvious. Sometimes, when I type a bad command in a bash terminal, the cursor just jumps down to the next line without any error or anything. I can't tell what I did wrong. It's like I'm stuck in the program. Reenactment:

$ tidy

Me: "Oops! That's not what I meant to type…"

:q

Me: "That didn't work…"

:exit
:quit
exit
quit
/exit
/quit
-exit
-quit
-wtf???

I know I screwed up but how do I get back to the prompt without closing the terminal?

Best Answer

You can always try the obvious things like ^C, ^D (eof), Escape etc., but if all fails I usually end up suspending the command with ^Z (Control-Z) which puts me back into the shell.

I then do a ps command and note the PID (process id) of the command and then issue a kill thePID (kill -9 thePID if the former didn't work) command to terminate the application.

Note that this is not a tidy (no pun intended) way to terminate the application/command and you run the risk of perhaps no saving some data etc.

An example (I'd have used tidy but I don't have it installed):

$ gnuplot

    G N U P L O T
    Version 4.2 patchlevel 6 
     ....
    Send bug reports and suggestions to <http://sourceforge.net/projects/gnuplot>

Terminal type set to 'wxt'
gnuplot> 
gnuplot>               #####  typed ^Z here
[1]+  Stopped                 gnuplot
$ ps
  PID TTY          TIME CMD
 1681 pts/1    00:00:00 tcsh
 1690 pts/1    00:00:00 bash
 1708 pts/1    00:00:00 gnuplot
 1709 pts/1    00:00:00 ps


$ kill 1708            ###### didn't kill the command as ps shows

$ ps
  PID TTY          TIME CMD
 1681 pts/1    00:00:00 tcsh
 1690 pts/1    00:00:00 bash
 1708 pts/1    00:00:00 gnuplot
 1710 pts/1    00:00:00 ps
$ kill -9 1708           ### -9 did the trick
$ 
[1]+  Killed                  gnuplot

$ ps
  PID TTY          TIME CMD
 1681 pts/1    00:00:00 tcsh
 1690 pts/1    00:00:00 bash
 1711 pts/1    00:00:00 ps