Python Handling User Input: read, Prompts & Secret Masking
The `read` builtin pauses script execution to capture keyboard input from standard input. It supports user prompts (`-p`), silent secret password entry (`-s`), countdown timeouts (`-t`), single-character keystroke confirmations (`-n 1`), and raw backslash protection (`-r`).
"`read` is the microphone of a bash script: it pauses the show, listens to what the user says into the keyboard, and stores their answer in a variable."
Deep Dive: How It Works
The Essential `-r` Flag: Always use `read -r` to prevent backslashes (`\`) from acting as escape characters.
Flags: `-p "prompt"` (display inline prompt), `-s` (silent / mask password keystrokes), `-t <secs>` (timeout after N seconds), `-n <count>` (return immediately after reading N characters).
Reading Lines from Files: `while IFS= read -r line; do ... done < file.txt` is the standard bulletproof pattern for parsing text files.
Syntax Blueprint
read -r -p "Enter username: " USERNAME read -r -s -p "Enter password: " PASSWORD; echo read -r -t 10 -p "Continue (y/n)? [10s timeout]: " CONFIRM read -r -n 1 -p "Press any key to proceed..."
Always include -r with read to prevent backslash stripping.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Forgetting `echo` after `read -s`, causing subsequent output to print on the same prompt line.Why it happens: Because -s suppresses the user's Enter key newline visually.
How to fix: Follow every `read -s` with a plain `echo` statement.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Parse Words with read
Echo "ZenCompiler Ready" and pipe it into `read -r TOOL STATUS` and echo "$TOOL: $STATUS".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.