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

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?



Solution 1:[1]

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 2:[2]

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.

Solution 3:[3]

On MacOS Terminal for me was enough to uncheck "Scroll alternate screen" for the issue to disappear. See screenshot of the preferences below.enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neuron
Solution 2 Sumith08
Solution 3 Ivan Carosati