Prompt

prompt_response

Prompt the user for a text response, with an optional default value. Loops until a non-empty response is given (or the default is accepted). Pass "-" as the default to allow an explicitly empty response.

Example

name=$(prompt_response "Enter your name" "World")
host=$(prompt_response "Hostname" "-")   # allows empty

Arguments

  • $1 (string): Prompt text
  • $2 (string): Default value (optional; use "-" to mean "empty is OK")

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • User's response (or default)

prompt_password

Prompt for a password, echoing '*' per keystroke and supporting backspace. Stores the result in the variable named by $1. The variable is declared readonly after assignment to prevent accidental modification.

Example

prompt_password db_pass "Database password"
printf '%s\n' "${db_pass}"

Arguments

  • $1 (string): Name of variable to store the password in
  • $2 (string): Prompt text

Exit codes

  • 0: Success; 2 Missing arguments

prompt_confirm

Prompt for an interactive yes/no confirmation. Reads a single character; only 'y' or 'Y' returns 0. Supports an optional timeout via -t or --timeout followed by a duration in seconds.

Arguments

  • $1 (string): Optional: '-t' or '--timeout' followed by timeout in seconds
  • $2 (int): Optional: timeout in seconds (when using -t/--timeout)
  • $3 (string): Optional: custom prompt text (default: "Continue")

Exit codes

  • 0: User confirmed with 'y' or 'Y'
  • 1: Any other input, or timeout expired

confirm

Alias for prompt_confirm.