read — Read a line of input into one or more shell variables interactively across all 5 shells
Equivalents in every shell
read -p "Name: " nameBash builtin. `-p` shows a prompt, `-r` disables backslash escapes (almost always wanted), `-s` hides input (passwords), `-t N` sets a timeout.
read "name?Name: "Zsh syntax for prompt is `"VAR?prompt"` (single quoted-arg form). Bash's `-p` flag also works.
read --prompt-str "Name: " nameFish uses long-form flags. `--prompt-str` for a static prompt; `--silent` (`-s`) for passwords; `--list` reads into an array variable.
$name = Read-Host -Prompt "Name"`Read-Host` reads a line. `-AsSecureString` returns a `SecureString` for password entry. The trailing `: ` after the prompt is added automatically.
set /p name=Name: `set /p` reads from stdin with no echo control (passwords are visible). The trailing space after `Name: ` is kept literally — important for prompt formatting.
Worked examples
Prompt for a name and greet
read -rp "Name: " name && echo "Hello, $name"read --prompt-str "Name: " name; echo "Hello, $name"$name = Read-Host "Name"; "Hello, $name"set /p name=Name: && echo Hello, %name%Read a password without echoing
read -rsp "Password: " pw && echoread --silent --prompt-str "Password: " pw$pw = Read-Host -Prompt "Password" -AsSecureStringRead each line of a file into a variable
while IFS= read -r line; do echo "$line"; done < file.txtwhile IFS= read -r line; do echo "$line"; done < file.txtGet-Content file.txt | ForEach-Object { $_ }Gotchas
- Always pass `-r` to `read` in bash/zsh. Without it, backslashes are treated as line-continuation/escape characters, so `read line` on input `C:\\Users` becomes `C:\Users` — destructive for Windows paths.
- Bash `read` with no `-r` and a trailing backslash on stdin hangs waiting for the next line. Easy to hit when piping output of `awk` or `printf` that wasn't escape-aware.
- `read VAR < file.txt` and `cat file.txt | read VAR` are NOT equivalent in bash: the pipe runs `read` in a subshell, so `$VAR` is empty after the pipe returns. Use input redirection (`< file.txt`) or process substitution (`< <(cmd)`).
- PowerShell `Read-Host -AsSecureString` returns a `SecureString` — to compare or hash it you must convert with `[Net.NetworkCredential]::new('', $pw).Password` or `ConvertFrom-SecureString`. Treat it as opaque until you need the plaintext.
- Cmd `set /p` reads up to (but not including) the newline; on empty input, the variable is NOT cleared — it keeps its previous value. Defensive scripts must `set var=` first to clear it.
WSL & PowerShell Core notes
Common tasks using read
- Loop over lines in a file
Iterate through a file one line at a time and run a command per line — for batch processing a hostnames list, replaying a SQL script line-by-line, or reacting to log lines.
- Prompt for yes/no confirmation
Block until the user confirms a destructive action — single-keystroke Y/N, defaulting to NO, case-insensitive, with a timeout so CI never hangs.
- Read user input from a prompt
Pause the script, show a prompt, and capture whatever the user types — for one-off prompts, simple wizards, and "please confirm" / "please enter your password" interactions.