Use "read" in Bash script with a standard answer

bash version 4+

You would write this:

read -p "enter a value: " -i default -e answer
echo "you answered: $answer"
  • -i default specifies the default answer.
  • -e enables interactive (editing) mode for read. Without this option the default answer does not work.

bash version < 4 (macos has bash 3.x)

So, can't edit the default value with bash 3.2. You could do this:

default="the default value"
read -p "your answer [default=$default] " answer
: ${answer:=$default}
echo "you answered: $answer"

This uses the default value if the user enters nothing (empty string)