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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…