22

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?

Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89

4 Answers4

28

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
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • Why not go the whole hog and use: `read -p "Enter IP address: " -i 192.168.0.4 -e IP`? – Jonathan Leffler Dec 18 '10 at 20:37
  • Good to know about `-i`. Might want to abstract out the default IP into a variable as well. – SiegeX Dec 18 '10 at 20:47
  • 1
    Good to know about combining `-e` with `-i` to pre-fill the reply buffer, but note that `-i` requires Bash 4.0 or higher (OSX users as of OSX 10.10 using the stock Bash version, bash 3.2.57, are out of luck). – mklement0 Jun 15 '15 at 17:21
20

The way I would do this is to suggest the default in the prompt in brackets and then use the default value parameter expansion to set IP to 192.168.0.4 if they just pressed enter, otherwise it will have the value they entered.

#!/bin/bash
default=192.168.0.4
read -p "Enter IP address [$default]: " IP
IP=${IP:-$default}
echo "IP is $IP"

Output

$ ./defip.sh
Enter IP address [192.168.0.4]:
IP is 192.168.0.4

$ ./defip.sh
Enter IP address [192.168.0.4]: 192.168.1.1
IP is 192.168.1.1
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 1
    Nicely done; this is the next best thing to `-e -i` if your Bash version is < 4. I suggest using a different variable name in your example, because `REPLY` has special meaning: it is set by Bash if you do _not_ specify a reply variable name, and its value is set to the _raw_ value entered, including any whitespace. – mklement0 Jun 15 '15 at 17:30
  • To me this is much cleaner than `-e -i` as it clears input from the screen. – vesperto Jun 25 '19 at 15:11
8

The classic way to do most of what you want is:

default="192.168.0.4"
echo -e "Enter IP address ($default): \c"
read reply
[ -z "$reply" ] && reply=$default
echo "Using: $reply"

This doesn't give the editing option.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Editing isn't practical but it's common to do something like:

echo -e "Enter IP address [$default]: \c"
read answer
if [ "$answer" = "" ]; then
     answer="$default"
fi
Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Yes. these ways are good but without editing :'(\n \n Why editing is useful for me:\n I have big questionnare. It can happen that user has made some mistakes and user wants to check out his answers. Than all questions are asked to user again but his last answer to every question is suggested automatically. I user made a little mistake (for example in last symbols) - he can edit it quickly – Larry Foobar Dec 18 '10 at 21:00