Why do I sometimes get repeatedly prompted with ">" in the terminal?

604

Solution 1

> is the default continuation prompt.That is what you will see if what you entered before had unbalanced quote marks.

As an example, type a single quote on the command line followed by a few enter keys:

$ '
> 
> 
> 

The continuation prompts will occur until you either

  • (a) complete the command with a closing quote mark

    or

  • (b) type Ctrl+D to finish input, at which point the shell will respond with an error message about the unbalanced quotes,

    or

  • (c) type Ctrl+C which will abort the command that you were entering.

How this is useful

Sometime, you may want to enter a string which contains embedded new lines. You can do that as follows:

$ paragraph='first line
> second line
> third line
> end'

Now, when we display that shell variable, you can see that the prompts have disappeared but the newlines are retained:

$ echo "$paragraph"
first line
second line
third line
end

Solution 2

That will happen if you have an unclosed quote in your command. That's something like:

$ echo "test here
>
>
...

You can exit that mode by closing the quote (write a " or ', or whatever your open quote is). It could also be a brace-delimited block, a partially-complete for-do or while-do loop, or certain other constructs. You can also press Ctrl-C to cancel this command (then press Up to revise it).

This can sometimes happen without an obvious missing quote when parameter or history expansions occur where you didn't expect them.


The > is your PS2 ("secondary prompt") value. You can change that to something else to remind you what's happened:

PS2="Unclosed >"

in your .bashrc will make it print Unclosed > at the start of each line instead.

Solution 3

In addition to the other answers, you also get the continuation prompt when you type a \ as the last character on a line.

Solution 4

The answer lies in this cryptic mention in the Bash Reference Manual:

5.1 Bourne Shell Variables

[...]

  • PS1: The primary prompt string. The default value is ‘\s-\v\$’. See Printing a Prompt, for the complete list of escape sequences that are expanded before PS1 is displayed.
  • PS2: The secondary prompt string. The default value is ‘>’.

followed by:

6.3.3 Interactive Shell Behavior

  1. Bash expands and displays PS1 before reading the first line of a command, and expands and displays PS2 before reading the second and subsequent lines of a multi-line command.

So, the > prompt appears if you press Enter and Bash determines that the command is incomplete. That could be because:

  • The character before the newline is a \, which is treated as a line continuation.
  • You have an incomplete string (mismatched quotes or unterminated here-doc) or some other mismatched delimiters, such as $(), (), ``.
  • You have started a function definition, a for loop, a while loop, or a case.

If you are seeing the secondary prompt due to an unintentional typing error, hit ControlC to return to the primary prompt.

Solution 5

The shell waiting for you to complete the command. Maybe there's a unclosed quote somewhere or it thinks you are starting a "for" loop and waits for the user to finish typing

Share:
604

Related videos on Youtube

makey
Author by

makey

Updated on September 18, 2022

Comments

  • makey
    makey over 1 year

    I'm using vim with tagbar plugin and by default tagbar shows tags for the current active buffer. I'm curious if there is an option to make it always show tags for the corresponding header file. So for example if the current buffer is foo.cpp I want tagbar to show tags for foo.hpp or foo.h. Is there such an option in tagbar or I have to code it myself?

    Thanks

  • Mr Lister
    Mr Lister over 9 years
    (For me, this usually happens by accident, as the backslash is frighteningly close to the Enter key.)
  • TRiG
    TRiG over 9 years
    Not on my keyboard, it isn't.
  • Barmar
    Barmar over 9 years
    You also get the secondary prompt when you're typing into a here-doc. But this is less likely to happen by accident than the others.