308

I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.

# Please enter your name: Ricardo^

In this script the prompt is "Please enter your name: " the default value is "Ricardo" and the cursor would be after the default value. Is there a way to do this in a bash script?

0

9 Answers 9

477

You can use parameter expansion, e.g.

read -p "Enter your name [Richard]: " name
name=${name:-Richard}
echo $name

Including the default value in the prompt between brackets is a fairly common convention

What does the :-Richard part do? From the bash manual:

${parameter:-word} If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Also worth noting that...

In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

So if you use webpath=${webpath:-~/httpdocs} you will get a result of /home/user/expanded/path/httpdocs not ~/httpdocs, etc.

Sign up to request clarification or add additional context in comments.

11 Comments

I ended up doing something based on this. Reading into a temp variable input and then using name=${input:-$name}.
This doesn't actually answer the question. The default value should be displayed in the prompt.
and what will name=${!input:-$name} do?
@Dr.PersonPersonII - you could add the default value by doing something like this: read -p "Enter the remote host name [$remote_host_default]: " remote_host remote_host=${remote_host:-$remote_host_default}
$1 becomes ${1:-some_default_string}
|
242
read -e -p "Enter Your Name:" -i "Ricardo" NAME

echo $NAME

Notes:

  • This option requires bash 4 or higher (bash --version)
  • On macos, install current bash using homebrew - brew.sh
  • -e and -i work together: -e uses readline, -i inserts text using readline
  • bash 4+ manual page for read - linuxcommand.org

7 Comments

Great answer! I just want to mention that I was having trouble with it, because I didn't see the space between "Ricardo" and NAME, but once I figured that out.... magic! Thank you!
unfortunately -i is is not a valid option for OSX 10.7
@BrianMortenson You can upgrade bash using homebrew: stackoverflow.com/questions/16416195/…
This answer shows how to make this work on OS X (Bash 3.x): stackoverflow.com/questions/22634065/…
Just note that -e seems to be mandatory to allow -i to actually work
|
63

In Bash 4:

name="Ricardo"
read -e -i "$name" -p "Please enter your name: " input
name="${input:-$name}"

This displays the name after the prompt like this:

Please enter your name: Ricardo

with the cursor at the end of the name and allows the user to edit it. The last line is optional and forces the name to be the original default if the user erases the input or default (submitting a null).

4 Comments

Can't use bash4 because it is non standard in debian distributions. I need something that will work without much hassle.
no need for the last line of code, just use name instead of input in the read command.
@RNAer: By using the extra variable, the value of $name is preserved if the user deletes the proposed value (and thus inputs a null string). It all depends on what your need is. I said as much in my answer. You're right, though, that I could have been more explicit and said that if the optional line were not used then the variable could have been name.
@DennisWilliamson: You are right. It's a good practice if that's what one wants.
20

I found this question, looking for a way to present something like:

Something interesting happened.  Proceed [Y/n/q]:

Using the above examples I deduced this:-

echo -n "Something interesting happened.  "
DEFAULT="y"
read -e -p "Proceed [Y/n/q]:" PROCEED
# adopt the default, if 'enter' given
PROCEED="${PROCEED:-${DEFAULT}}"
# change to lower case to simplify following if
PROCEED="${PROCEED,,}"
# condition for specific letter
if [ "${PROCEED}" == "q" ] ; then
  echo "Quitting"
  exit
# condition for non specific letter (ie anything other than q/y)
# if you want to have the active 'y' code in the last section
elif [ "${PROCEED}" != "y" ] ; then
  echo "Not Proceeding"
else
  echo "Proceeding"
  # do proceeding code in here
fi

Hope that helps someone to not have to think out the logic, if they encounter the same problem

2 Comments

Very nice. It could be even improved a little to repeat the question if someone just enters "k" or something else than the given choices.
I just wrote this comment following a scratched itch. I now have a shell function, which includes the above, and I tend to use that. Shell doesn't seem to loop nicely, so I try to avoid it. I might test the result from my function, and give one more try, but basically, I'm writing admin tools where things might go wrong, so I want the admin to be able to stop the script cleanly at any point.
18

Code:

IN_PATH_DEFAULT="/tmp/input.txt"
read -p "Please enter IN_PATH [$IN_PATH_DEFAULT]: " IN_PATH
IN_PATH="${IN_PATH:-$IN_PATH_DEFAULT}"

OUT_PATH_DEFAULT="/tmp/output.txt"
read -p "Please enter OUT_PATH [$OUT_PATH_DEFAULT]: " OUT_PATH
OUT_PATH="${OUT_PATH:-$OUT_PATH_DEFAULT}"

echo "Input: $IN_PATH Output: $OUT_PATH"

Sample run:

Please enter IN_PATH [/tmp/input.txt]: 
Please enter OUT_PATH [/tmp/output.txt]: ~/out.txt
Input: /tmp/input.txt Output: ~/out.txt

1 Comment

I like this because it follows the convention that I see in a lot of shell scripts where the default value is in square brackets before the colon. Thanks!!!
12

I've just used this pattern, which I prefer:

read name || name='(nobody)'

1 Comment

I tried this on zsh and bash and it didn't work in either shell.
10
name=Ricardo
echo "Please enter your name: $name \c"
read newname
[ -n "$newname" ] && name=$newname

Set the default; print it; read a new value; if there is a new value, use it in place of the default. There is (or was) some variations between shells and systems on how to suppress a newline at the end of a prompt. The '\c' notation seems to work on MacOS X 10.6.3 with a 3.x bash, and works on most variants of Unix derived from System V, using Bourne or Korn shells.

Also note that the user would probably not realize what is going on behind the scenes; their new data would be entered after the name already on the screen. It might be better to format it:

echo "Please enter your name ($name): \c"

4 Comments

printf is more portable than echo
@ghostdog74: maybe so; those of us who learned shell programming over 25 years ago have a hard time working out which of our practices are still relevant. It increasingly looks like bash has rewritten just about everything. I know printf has been around a while as a command - I very seldom use it, though (probably - never?). I get the impression I should shut up on shell until I've (re)learned bash. 'Tis funny; the software I work on has shell scripts that don't work well - but the trouble isn't bash-isms. I just get fed up with fixing 'if (test -z "$xxx"); ...' and other C shellisms.
I'm amazed that bash supports \c, since it also supports echo -n! However, you do have to add -e to get bash's echo to interpret escapes. I guess \c is for things left unsaid: echo -e "Syntax slightly off\c, but I've learned so much from what you've shared. Thanks, @JonathanLeffler!"
@Jonathan Leffler Unfortunately, not really what the OP asked for, at least not on my bash version 4.x $ bash --version GNU bash, version 4.3.33(0)-release a) "I would like to be able to provide a default value that the user can change." Your echo "Please enter your name: $name \c" does not enable me to edit the default value. b) "and the cursor would be after the default value." Not true, either... The answer by @Paused until further notice. does fulfill both requirements.
1
#Script for calculating various values in MB
echo "Please enter some input: "
read input_variable
echo $input_variable | awk '{ foo = $1 / 1024 / 1024 ; print foo "MB" }'

1 Comment

please add explanation to your answer.
0

The -e and -t parameter does not work together. i tried some expressions and the result was the following code snippet :

QMESSAGE="SHOULD I DO YES OR NO"
YMESSAGE="I DO"
NMESSAGE="I DO NOT"
FMESSAGE="PLEASE ENTER Y or N"
COUNTDOWN=2
DEFAULTVALUE=n
#----------------------------------------------------------------#
function REQUEST ()
{
read -n1 -t$COUNTDOWN -p "$QMESSAGE ? Y/N " INPUT
    INPUT=${INPUT:-$DEFAULTVALUE}
    if  [ "$INPUT" = "y" -o "$INPUT" = "Y" ] ;then
        echo -e "\n$YMESSAGE\n"
        #COMMANDEXECUTION
    elif    [ "$INPUT" = "n" -o "$INPUT" = "N" ] ;then
        echo -e "\n$NMESSAGE\n"
        #COMMANDEXECUTION
    else
        echo -e "\n$FMESSAGE\n"
    REQUEST
    fi
}
REQUEST

5 Comments

Your answer has not much to do with the question, has it?…
its a work around for using the -e and -t parameter. this codeline ( -e and -i for default value ) : read -e -p "Enter Your Name:" -i "Ricardo" NAME does not work with a countdowntimer ( -t )
Yeah but that's not asked in the question, is it?
What you can do is ask a new question, and answer it yourself ;). This is allowed on SO! but we don't want to pollute other questions with unrelated stuff.
ironically, this was exactly the answer I needed for ensuring a default value, in my case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.