Suggest answer to user input in bash scripting

Larry Cinnabar picture Larry Cinnabar · Dec 18, 2010 · Viewed 13.3k times · Source

Here is an example:

#!/bin/bash
echo -e "Enter IP address: \c"
read
echo $REPLY

But I want to make it easier for the user to answer. I'd like to offer an answer to the user. It should look something like this:

Enter your IP: 192.168.0.4

And the user can just press Enter and agree with 192.168.0.4, or can delete some characters (for example delete "4" with one backspace and type 3 instead).

How to make such an input? It is possible in bash?

Answer

Martin v. Löwis picture Martin v. Löwis · Dec 18, 2010

bash's read has readline support (Edit: Jonathan Leffler suggests to put the prompt into read as well)

#!/bin/bash
read -p "Enter IP address: " -e -i 192.168.0.4 IP
echo $IP