Read input from stdin or user prompt into shell variables.
low risk
read captures user input or reads lines from stdin into shell variables. Used for interactive scripts, parsing file content line-by-line, and prompting for secrets (with -s to disable echo). The -r flag prevents backslash interpretation.
| Flag | Meaning |
|---|---|
-r --raw | Raw mode: don't interpret backslashes |
-s --silent | Silent mode: don't echo input (for passwords) |
-p --prompt | Print prompt before reading |
-t --timeout | Timeout in seconds |
-a --array | Read words into an array |
read -p 'Enter cluster name: ' CLUSTERInteractive prompt with variable capture · explain & risk-check →read -s -p 'Password: ' PASSSilent password prompt · explain & risk-check →while IFS=: read -r user _ uid _; do echo "$user uid=$uid"; done < /etc/passwdParse /etc/passwd field by field · explain & risk-check →read -r -t 10 -p 'Continue? [y/N] ' responsePrompt with 10-second timeout · explain & risk-check →Always use `IFS= read -r line` when reading file lines — prevents leading/trailing whitespace stripping and backslash interpretation that causes subtle bugs.
Reference text is hand-written and may be incomplete — check man read for your version. Test destructive commands on a non-production system first.