ask for input in a script
Ask yes or no, read a password without showing it, or wait for an answer with a time limit.
Steps
-
read -rp 'Continue? [y/N] ' answerask, and keep the answer
-
[[ $answer == [yY]* ]] || exit 1stop unless it starts with y
-
read -rsp 'Password: ' password; echoread a password without showing it
-
read -rt 10 -p 'Name: ' name || name=guestwait 10 seconds at most
Commands used
read
Each has a page in lore: lore read opens it.
Related recipes
- check if a file or folder existsTest in a script whether a path exists, is a file, a folder, empty or runnable.
- check if a string contains a textTest in a script whether a string contains, starts with or matches something.
- find the script's own folderInside a script, find the folder the script is in, to reach files next to it.
- get a file's name, folder and extensionTake a path apart in a script: the folder, the name, the name without its extension, the extension.