Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

91,814

Solution 1

Because that's what the keyboard actually sends to the PC (more precisely, what the terminal prints for what it actually receives from the keyboard). bash for example gets those values, deciphers them and understands that you want to move around, so it will either move the cursor (in case of left/right) or use its history to fetch previous commands (up/down). So you can't expect your program to magically support arrow keys.

However, reading from standard input from the terminal already supports left/right arrow keys (I believe, but I'm not in Linux right now to test and make sure). So my guess is that there is another issue interfering. One possible cause could be that one of your modifier keys is stuck? Perhaps ALT, CTRL or SUPER?

Solution 2

For those who are coming from the osx (mac) try changing the shells to bash

Terminal -> Preferences -> Shells open with -> [select] Command (complete path)

then paste

/bin/bash

Solution 3

This might be because the user account is created in shell. You can change it to bash by two ways.

Permament solution is -

sudo chsh -s /bin/bash ${username}

To get this solution working you will have to logout and login

Temporary solution is everytime when you login into the ubuntu server type bash and hit return.

Share:
91,814
Kevin Dong
Author by

Kevin Dong

Nice to meet you. (・ω ・ )

Updated on February 08, 2022

Comments

  • Kevin Dong
    Kevin Dong about 2 years

    I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.

    The program is simple:

    • read the line from console with scanf().
    • Analyze the string and calculate.

    But something weird happens. When I try to move the cursor, it prints four characters:

    • pressing Up prints "^[[A"
    • pressing Dn prints "^[[B"
    • pressing Rt prints "^[[C"
    • pressing Lt prints "^[[D"

    • How can this be avoided?

    • Why does it print these 4 characters instead of moving the cursor?

  • Kevin Dong
    Kevin Dong about 10 years
    When I press arrow keys in terminal, it'll move the cursor, but when reading input from buffer with scanf(), it can't.
  • Shahbaz
    Shahbaz about 10 years
    @KevinDongNaiJia, it could be that bash detects the modifier and ignores them. Unfortunately, right now I'm in Windows, so I can't really tell. Just to be sure, you using the normal gnome-terminal and have not modified any settings, either of the terminal or of the keyboard (in Ubuntu settings)?
  • Kevin Dong
    Kevin Dong about 10 years
    Yes, I tested it in normal gnome-terminal without any settings. I just used Ubuntu in the second day. ;-)
  • Shahbaz
    Shahbaz about 10 years
    What is your keyboard's layout? Try changing its layout to "Enlish (US)" and see if the problem persists.
  • Kevin Dong
    Kevin Dong about 10 years
    Nothing changed when using "English (US)" keyboard layout. And I tried to execute it in tty1~tty6, this problem still happened.
  • Shahbaz
    Shahbaz about 10 years
    This is really weird, what is your keyboard brand/model? Does this happen in the terminal of Windows if you have one? What if you attach this keyboard to someone else's computer with Ubuntu? Do you still see the same behavior?
  • Kevin Dong
    Kevin Dong about 10 years
    I use notebook, so the keyboard is built-in. Then I used USB-keyboard (English (US) Layout), but the problem still existed. Last, I tested it in other Ubuntu derivative, Kubuntu, and this problem didn't solve.
  • Shahbaz
    Shahbaz about 10 years
    This very likely some incompatibility between something in your laptop and Ubuntu (you can thank Microsoft's monopoly for that). Try asking on askubuntu.com, perhaps someone there has experienced something like that. Make sure to include the laptop brand and model
  • chichilatte
    chichilatte over 5 years
    I was trying to exec into a docker instance on OSX (using Kitematic). This worked a treat, thank you. The Kitematic preferences let you set the shell to bash.
  • cmaster - reinstate monica
    cmaster - reinstate monica over 5 years
    Actually, the problem seems to be the mode of the terminal: The terminal has different modes, like either directly sending each character to the process, or aggregating a line until the user presses enter, or displaying stuff with history scroll-back as opposed to vim style full-screen mode. There is a lot of stuff that's going on, and I don't have the time right now to look up the details (which is why I'm not posting an answer myself). Anyway, when you see ^[[A appear on the screen, that's the escape sequence sent to represent the up arrow, and nothing interpreted it as such.
  • Dio Phung
    Dio Phung almost 4 years
    In my case, I have to use sudo chsh -s /bin/bash $(whoami).
  • Roel Van de Paar
    Roel Van de Paar over 3 years
    Perfect answer. Was looking for a bit.
  • Blindleistung
    Blindleistung over 3 years
    The symbols ^[[A are a common problem (exactly these chars) when trying to use the line-edit and the shell mismatches. It is definitely not a problem about stuck keys such as ALT. answer from cmaster looks good.