6

In sh (not Bash), how would we abort execution of command when the prompt is in > mode?

For example, when entering a string with quotes only at the beginning, it makes the prompt look like >, without the ability to quit it normally, unless hitting Ctrl + D. Example:

root@MyPC:~# echo "hello
> I am
> (How do I exit of this mode?)

In case that I don't know which character delimits the string (" or ' or simply escaping a newline/spaces with backslash), is there a way to let Bash know that I want to abort the execution of the current command?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Dor
  • 2,445
  • 7
  • 33
  • 32
  • Related: [In which situations are PS2, PS3, PS4 used as the prompt?](http://unix.stackexchange.com/q/193659/117037) – wjandrea Mar 08 '17 at 23:20

1 Answers1

18

^C aka Ctrl+C will abort what you're doing and get you back to a normal prompt.

Kiwy
  • 9,415
  • 13
  • 49
  • 79
Flup
  • 8,017
  • 2
  • 33
  • 50
  • 2
    A good thing to know in general for interrupting any running command. – orion Mar 05 '14 at 10:09
  • 2
    if it does not work try also crtl + d to stop process and if does not work use ctrl + z to background the running command find it's PID and kill it with `kill` – Kiwy Mar 05 '14 at 10:54
  • `^D` will only work if a process is reading from standard input. `^\ ` is a good one too (sends a `SIGQUIT`). – Flup Mar 05 '14 at 11:01
  • 2
    @Kiwy Easier is to run `jobs` and use `kill %x` – Bernhard Mar 05 '14 at 12:19
  • Is this true also when connecting with `telnet` or `cu` to Linux that runs on embedded system? For some reason you solution does work, but for regular shells (of PCs). On an embedded system, only `CTRL+D` works. – Dor Mar 05 '14 at 15:00
  • 1
    It depends what `intr` is set to on the tty. If you do `stty -a` you'll see which control codes are set to do what. – Flup Mar 05 '14 at 15:32
  • To clarify, Ctrl+D just means End-Of-File (close input stream) and if the command reads your input, it just finishes normally because there is nothing more to do. If Ctrl+C works, it's the standard solution for instant interruption (a known "twitch" of a frustrated unix user whenever anything goes wrong is just to repeatedly do this). It sends SIGINT, and in an interactive shell, possibly SIGTERM if SIGINT doesn't work. kill is more suited if your process is running asynchronously and you don't interact directly. – orion Mar 05 '14 at 15:47
  • @Flup: The binary `stty` doesn't exists :s Is there a different way? – Dor Mar 08 '14 at 23:20
  • Really? I've never come across a system lacking `stty`. Tried `/bin/stty`? – Flup Mar 10 '14 at 08:08
  • sometimes when ctrl is blocked while using arcos presentation or browser based remote, simply complete the multiline `"` – z2z Apr 02 '20 at 19:59