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.

  • shell
  • development

Steps

  1. read -rp 'Continue? [y/N] ' answer

    ask, and keep the answer

  2. [[ $answer == [yY]* ]] || exit 1

    stop unless it starts with y

  3. read -rsp 'Password: ' password; echo

    read a password without showing it

  4. read -rt 10 -p 'Name: ' name || name=guest

    wait 10 seconds at most

Commands used

  • read

Each has a page in lore: lore read opens it.