0

I am using the following code to take input from user interactively, thus allowing the user to make changes to the input without deleting the entire line.

#! /bin/bash

while :
do
    echo -n "prompt# "
    read -e input
done

Problem:

It also allows me to delete the "prompt#", which I don't want to happen. I don't want the "prompt# " to be deleted by the user in any case. I've tried using various options in read command but can't figure it out. How can I achieve this?

Edit:
The sequence of events that led to the problem:
1. I ran the above script and entered "hello".
2. Then I moved my cursor to 'e' in "hello" and "pressed and held" backspace for some time which resulted in the deletion of the "prompt# "
3. If you don't type any characters it works fine and the prompt is not deleted but if you enter even a white space or any character and then press the backspace then it messes the prompt.

Take a look at the snapshot below.
Snapshotbash readline prompt deletion:

10
  • 5
    You should use read's builtin prompt support, or you're on your own. read -p 'prompt# ' -e input. Commented Jan 7, 2016 at 4:43
  • @4ae1e1: This is exactly what I wanted. Thank you very much.... Commented Jan 7, 2016 at 4:48
  • 1
    @4ae1e1 - make it an answer! Commented Jan 7, 2016 at 5:41
  • I agree with using read -p. But even with OP's current implementation, it should not allow deleting the lines echoed by echo, unless user enters some ANSI control characters... Commented Jan 7, 2016 at 6:03
  • @anishsane I'd like to claim that it shouldn't, but I can't because it's highly reproducible (and I vaguely remember running into this problem before myself). I can reproduce on latest stable (4.3.42, readline 6.3.8) and testing (4.4-beta with bundled readline). Maybe it's a bug in readline. Note that there's no "deleting the lines" involved; you can delete (or move) to the beginning of the current line, that's it. Commented Jan 7, 2016 at 6:34

1 Answer 1

2

From 4ae1e1's comment:

You should use read's builtin prompt support, or you're on your own.

read -p 'prompt# ' -e input
Sign up to request clarification or add additional context in comments.

1 Comment

Off topic: Many times, some user posts the answer as comment & then forgets about it. But the question does not get closed, because there is no answer. & I don't want to steal credit of that user. So, marking it as community wiki is a good idea.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.