32

Whenever I'm at the console login, I press up arrow intentionally to see the previously typed commands. But I see this ^[[A.

But when I press Ctrl Alt Print Screen Scroll Lock Pause Break Page Up Page Down Win keys doesn't echo any characters.

What might be the reason behind?

Does ^[[A sort of characters imply anything?

enter image description here

20

That's the way that the terminal represents the raw keycode of the Up key sent to it by the keyboard. Basically, your shell would normally intercept the keypress, but there's nothing to do that at the login prompt. So the character that you typed gets printed to the console just like any other letter (or number, or whatever).

  • 3
    @RubanSavvy Ctrl, Alt and Win (often called Super in the GNU/Linux world) are modifiers. If you press them and a key, you'll see something different printed on the screen. Scroll lock I suspect is interpreted by the kernel or something else low level, for tty control, although I'm not sure. Page up and down are probably swallowed by getty or login, although I'm not sure why. Educated guessing says that print screen is interpreted specially by the kernel for historic reasons. I'm not 100% sure on any, though, except the modifiers. – strugee Dec 4 '13 at 7:14
  • 3
    the above should probably be edited into the answer. – strugee Dec 4 '13 at 7:14
  • related discussion in chat: chat.stackexchange.com/transcript/message/12481097#12481097 – strugee Dec 5 '13 at 0:59
19

Keyboards send events to the computer. An event says “scan code nnn down” or “scan code nnn up”. At the other end of the chain, applications running in a terminal expect input in the form of a sequence of characters. (Unless they've requested raw access, like the X server does.) When you press A, the keyboard sends the information “scan code 38 down”. The console driver looks up its keymap and transforms this into “character a” (if no modifier key is pressed).

When you press a key or key combination that doesn't result in a character, the information needs to be encoded in terms of characters. A few keys and key combinations have corresponding control characters, e.g. Ctrl+A sends the character (byte value 1), Return sends the character (Ctrl+M, byte value 13), etc. Most function keys don't have a corresponding character and instead send a sequence of characters that starts with the (escape, byte value 27) character. For example, the key Up is translated into the escape sequence ␛[A (three characters: escape, open bracket, capital A).

The user name prompt on the console is dumb and doesn't understand most escape sequences. It doesn't have the line edition and history features that you're used to: those are provided by the shell, and until you log in, you don't have a shell. So it simply displays the escape sequence. There is no glyph for the character, so it's displayed as ^[. The ^ sign is traditionally used as a prefix for control characters, and escape is ^[ because of its byte value: it's the byte value of [, minus 64.

If you press Up at a shell prompt, this sends the same 3-character sequence to your shell. The shell interprets this as a command sequence (typically to recall the previous history item). If you press Ctrl+V then Up at a shell prompt, this inserts the escape sequence at the prompt: Ctrl+V is a command to insert the next character literally instead of interpreting it as a command, so the character is not interpreted as the start of an escape sequence.

Some keys are only modifiers and are not transmitted to terminal applications. For example, when you press Shift, this information stays in the terminal driver, and is taken into account if you then press A, so the driver sends A to the application instead of a.

Additionally some function keys may not be mapped in your console.

For a similar view in the GUI, see What is bash's meta key?

  • This is a great answer, thanks Gilles as always. – JoshuaRLi Jun 13 at 20:55
5

It's not about how the keys are represented by the "terminal" (i.e., the terminal emulator application). What you're seeing is the ANSI code (ANSI escape sequence) for moving upward one line, but translated into printable form.

  1. Keyboard hardware sends "scan codes", but they are translated and presented to commandline-level applications as characters. The key A becomes a single byte: A if the Shift key is down (or Shift Lock), a otherwise.

  2. In an ANSI-compliant terminal, the arrow keys don't send a single character (there are no codes for arrows in the ASCII character set), but a 3-character "escape sequence": escape-[-A. The other three arrow keys are escape-[-B, C, D.

  3. The same character sequence would move the cursor up by one line if sent (echo'ed) to an old physical ANSI terminal. Many programs, including terminal emulators, recognize these character sequences and do something appropriate: terminal emulators will move the cursor up (this is how the curses library moves the cursor around), but bash will intercept it and scroll the history instead.

  4. To avoid having the cursor end up all over the place in programs that have no use for moving the cursor around the screen, you will often see ESC in keyboard input displayed as the printable sequence ^[ (because escape corresponds to control-[). This is actually handled by the terminal device interface; see stty(1). As a result, the up arrow will show up as ^[[A. You will see this from the command line if you type cat, hit return, and press some arrow keys. This is also what you saw on the console login screen.

Finally: Control, Alt, and the other keys you mentioned do not map to character sequences. They affect the character sent by another keypress (like the a/A example above), or they simply have no mapping to text. Such keypresses can only be detected by programs that listen for keyboard events. They cannot be seen by reading from standard input (or be written to a file).

3

This behavoir is different from shell to shell. Most shells use a library called readline to edit lines in the prompt. Here is a complete command reference for this library, so when an application is using readline you can edit and navigate the lines with this commands.

The vertical arrow keys are configured in readline to navigate the command history. And in the login promt there is no command history. That's why the characters ^[[A and ^[[B are printed on the screen. So what does ^[[A mean?

The manual page of bash says under PROMPTING:

\[     begin a sequence of non-printing characters, which could be used to embed
       a terminal control sequence into the prompt

The escape sequences for the arrow keys in ANSI are:

  • [ValueA Cursor up (where Value can be emtpy)

  • [ValueB Cursor down (where Value can be emtpy)

  • 5
    Your answer is about different thing, because the login prompt is printed and handled by login, which is not related to bash. – Risto Salminen Dec 4 '13 at 7:55
  • 1
    bash and login are using the same library (readline) to edit lines. The difference is just that login don't interprete the vertical arrow keys as a command, like bash does. – chaos Dec 4 '13 at 9:16
  • Yes, I admit that. – Risto Salminen Dec 4 '13 at 10:53
  • The bash syntax is meant to look like the ^[ representation of escape, but they're different things. Bash could have used any syntax at all. In fact, when you write \[A, bash outputs three characters: ^[ (i.e. escape), [, A. – alexis Dec 11 '13 at 9:47
0

Those are ANSI escape codes. ^[ is the notation your shell uses to display an ESC byte (ASCII byte 27). So your example is an ESC byte followed by the text [A. As you can see in the Wikipedia article, ^[[ (ESC followed by [) is a Control Sequence Introducer or CSI. CSI A means move the cursor up by one column.

If you want to see an escape code in the terminal, type CTRL+V and then another key sequence. For example, CTRL+V followed by the up arrow shows ^[[A.

0

The command shell, for example Bash is the program that translate arrow up into the action "retrieve previous command". You don't have a command shell running.

0

TLDR

You're probably running sh which outputs the raw keycodes generated when you press the Up arrow key.

A more advanced shell like bash intercepts these keycodes and does something with them. E.g. show the last command in its history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.