How to present editable input to a user with BASH and ZSH?

Viewed 490

I need to accept user input within a shell script that can run both in BASH and ZSH. I'm accustomed to using readline in other languages, but this doesn't seem to be a viable option in shell scripting.

An example prompt might be:

Please enter the value> 1234_

How can I present a user with editable input that has a default value that can be edited (backspaced) that's compatible with both shells?

2 Answers

User @cyrus mentioned in the comments, but is better directly as an answer instead:

read -e -p 'Please enter the value> ' -i '1234' myvar
echo $myvar

results in an editable default of 1234

Please enter the value> 1234
1234

If I did not misunderstand ... In terms of "Backspace" functionality, to erase the last characters and add some new .. This works in both shells with a simple "read" in the script.

echo -n "Enter value > "
read v
echo $v

If you have other issues, could you kindly rephrase to make your question clearer ? What do you use (Example Code). What do you perform exactly in terms of editing (only BS or do you mean line editing commands CTRL-A, etc) What is the result / error, what you want to get fixed. Thanks.

Related