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?
You can use parameter expansion, e.g.
Including the default value in the prompt between brackets is a fairly common convention
What does the
:-Richardpart do? From the bash manual:Also worth noting that…
So if you use
webpath=${webpath:-~/httpdocs}you will get a result of/home/user/expanded/path/httpdocsnot~/httpdocs, etc.