Skip to main content

How to Stop a Running Command in the Terminal with Ctrl+C in Bash and Zsh

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Some commands don't end on their own: a web server, a log that keeps streaming, a script stuck waiting. Pressing Ctrl+C in the terminal asks the running command to stop, and gives you your prompt back. This guide stops a command in bash and in zsh and shows what it leaves behind.

Syntax​

Keys
Ctrl+C
PartWhat it means
Ctrl+CHold Control and press C while a command is running in the foreground. On a Mac it's Control, not Command
^CHow the terminal shows that you pressed it
$?Afterwards, the exit status of the stopped command

Ctrl+C sends the running command a signal named SIGINT, interrupt. The zsh manual explains the number you see afterwards: "The value of a simple command is its exit status, or 128 plus the signal number if terminated by a signal."

The Example​

sleep 60 waits a minute before it ends, which stands in for any long-running command. You stop it after two seconds and then print its exit status.

Step 1: Start a Command and Stop It​

Terminal
sleep 60

Press Ctrl+C while it's waiting. Then check how it ended:

Terminal
echo "exit status: $?"
Output in bash
$ sleep 60
^C
$ echo "exit status: $?"
exit status: 130

A bash terminal: sleep 60, then ^C where Ctrl+C was pressed, then echo exit status, which prints exit status: 130.

zsh shows the same thing: ^C, then exit status: 130.

The terminal printed ^C where you pressed the keys, sleep stopped without waiting out its minute, and the prompt came back. The status 130 is 128 plus 2, and 2 is SIGINT's number: kill -l INT prints 2 in both shells.

Common Mistakes​

MistakeWhat happensDo this instead
Pressing Command+C on a MacThat copies text. The command keeps runningUse the Control key
Closing the window to stop a serverThe server may not get to stop cleanlyPress Ctrl+C first, and wait for the prompt
Pressing Ctrl+C with no command runningNothing to stop: the shell just shows a fresh promptOnly needed while something runs

Troubleshooting​

SymptomLikely causeFix
^C appears but the prompt doesn't come backThe program is still finishing its shutdown, or ignores the signalWait a few seconds. If it stays stuck, stop it from another terminal
A later command reports exit status 130The previous command was stopped with Ctrl+CNothing to fix: that's what 130 means

FAQ​

Does Ctrl+C always stop a program?​

It asks it to stop. Most programs stop at once. Some catch the signal to clean up first, such as servers that shut down their parts one by one, so they take a moment.

Conclusion​

Ctrl+C stopped sleep 60 after two seconds, the terminal showed ^C, and $? was 130, which is 128 plus SIGINT's number 2. Use it to stop servers and stuck commands, and give them a moment to shut down.

Additional Resources​

  • zsh shell grammar — exit statuses of commands ended by a signal.
  • man 7 signal on Linux, or man signal on macOS — the list of signals.