Abhishek Choubey
Abhishek Choubey

Reputation: 883

Reading lines interactively in bash

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:

Upvotes: 0

Views: 93

Answers (1)

WilQu
WilQu

Reputation: 7423

From 4ae1e1's comment:

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

read -p 'prompt# ' -e input

Upvotes: 2

Related Questions