Read a variable in bash with a default value

BashShell

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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

Solution 3 - Bash

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).

Solution 4 - Bash

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

Solution 5 - Bash

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

Solution 6 - Bash

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

read name || name='(nobody)'

Solution 7 - Bash

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"

Solution 8 - Bash

#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" }'

Solution 9 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRicardo MarimonView Question on Stackoverflow
Solution 1 - Bashghostdog74View Answer on Stackoverflow
Solution 2 - BashJadav BhedaView Answer on Stackoverflow
Solution 3 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 4 - Bashmanish_sView Answer on Stackoverflow
Solution 5 - BashsibazView Answer on Stackoverflow
Solution 6 - BashbukzorView Answer on Stackoverflow
Solution 7 - BashJonathan LefflerView Answer on Stackoverflow
Solution 8 - BashkokaneView Answer on Stackoverflow
Solution 9 - BashspeefakView Answer on Stackoverflow