How do I exit or cancel a bad bash command?

484,637

Solution 1

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

Solution 2

Try pressing Ctrl-D or Ctrl-C. If it fails, kill the process .

Trying with the tidy command you mentioned, Ctrl-D works.

Solution 3

Another solution (not mentioned already) is to send the SIGQUIT signal using ctrl+\

It is stronger than a ctrl+c

Solution 4

CTRL+D == exit shell command

and

CTRL+C == terminate the current process, Of course may be the given software handle it and CTRL+D doens't work

Of course , They produce a kernel signal if you want to know more, read :

man 7 signal
Share:
484,637

Related videos on Youtube

DevWouter
Author by

DevWouter

Updated on September 18, 2022

Comments

  • DevWouter
    DevWouter over 1 year

    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?

  • DevWouter
    DevWouter almost 12 years
    That's it!! ^Z to the rescue. Thank you so much.
  • Levon
    Levon almost 12 years
    @davidkennedy85 Happy to help ... I was just cobbling together an example to post
  • Alan Curry
    Alan Curry almost 12 years
    Shh! Don't tell people about the ^\ or they'll start SIG_IGN'ing that too, then what will we do?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 12 years
    Ctrl+D is end-of-file, not directly exit.
  • godlygeek
    godlygeek over 9 years
    I'd recommend ^Z, then kill %% to kill the job nicely, then kill -9 %% to kill it harshly if necessary. And using %% instead of using ps to find a pid is much easier.
  • Noumenon
    Noumenon over 8 years
    @godlygeek Can you tell me what %% means in Linux or what kind of thing it is? I searched a lot.
  • Wildcard
    Wildcard about 8 years
    @Noumenon, have a look at LESS='+/^JOB CONTROL' man bash. %% refers to the "current job".
  • 12431234123412341234123
    12431234123412341234123 over 7 years
    kill -KILL <pid> or kill -SIGKILL <pid> is more readable than kill -9 <pid>
  • marsh
    marsh almost 7 years
    What is the ^ key?
  • Levon
    Levon almost 7 years
    @marsh ^ is for control key, so ^Z would be Control-Z, ^C -> Control-C, etc .. ^Z suspends a command, ^C kills/terminates it. Hope that helps.
  • T.Todua
    T.Todua about 6 years
    CTRL+C is good for windows CMD too.