8

I'm running very simple script which reads line by line and prints entered line back to terminal:

while read CMD; do
    echo $CMD
done

It works fine, but when I'm trying to edit line with backspace character it prints ^?instead of deleting character. And when I press Ctrl + backspace the output is ^H. How could this behavior be fixed so backspace removes character instead of adding these characters? I tried stty erase '^?' command but not successfully.

17
  • Please note 16.10 reached the end of its support cycle last month. You should upgrade to 17.04 as soon as possible.
    – Byte Commander
    Aug 15, 2017 at 16:22
  • 1
    I suggest to replace read CMD by read -e CMD.
    – Cyrus
    Aug 15, 2017 at 19:26
  • 1
    try terminator instead of gnome-terminal . Its working as expected Aug 21, 2017 at 8:58
  • 1
    @whtyger this worked for this script. In my real situation I have input from program, which doesn't call read. I have no opportunity to change the program script, this was just an example which represented my issue. Is there any global settings where I can have this configurations done for all input?
    – vrom911
    Aug 22, 2017 at 14:33
  • 1
    @Cyrus My Backspace works as I want — it deletes last character always, even for this script from question without -e option. But on @vrom911's machine it's not the case. I wonder why it happens and where this can be configured.
    – Shersh
    Aug 22, 2017 at 14:41

2 Answers 2

6

The problem is that there are many ways to represent a backspace, but the read command doesn't understand all of them. You should be able to configure what exactly is sent when pressing the ← Backspace key in your terminal emulator's settings.

Assuming you're using gnome-terminal as emulator, open the Edit manu and click on Profile preferences. Switch to the Compatibility tab and you should get these options:

gnome-terminal → profile preferences → compatibility

Now you can change the Backspace key generates: setting. You have these five options below available to chose from. I added in braces how they behave on my system:

  • Automatic (works)
  • Control-H (prints ^H instead)
  • ASCII DEL (default, works)
  • Escape sequence (prints ^[[3~ instead)
  • TTY Erase (works)
3
  • Currently I have ASCII DEL. I have tried all 5 options but none of them fixed the issue.
    – vrom911
    Aug 15, 2017 at 16:42
  • That is strange. Can you tell my what type read says and what shell/version $SHELL --version reports?
    – Byte Commander
    Aug 15, 2017 at 16:53
  • I know it is strange. I tried to google my problem a lot but didn't find solution that works for me. type read says read is a shell builtin. and $SHELL --version says GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
    – vrom911
    Aug 15, 2017 at 17:42
1

I had the same problem, using bash in Gnome-Terminal.

Note that on the command line, the backspace works as expected, but when my program (a simple TCP socket based chat client) is reading from stdin, the backspace character prints (echoes back to the screen) ^?instead of deleting the last character.

Use # stty icanon to allow the "canonical (cooked) mode". See some explanation here https://unix.stackexchange.com/questions/131105/how-to-read-over-4k-input-without-new-lines-on-a-terminal

Some more background to my problem/solution: I had a problem where I need to paste more than 4k characters. And during that research, I was led to the above link. I used # stty -icanon to disable the 4k limit, but lost the ability to use backspace (erase).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .