Skip to content
shellmap

readRead a line of input into one or more shell variables interactively across all 5 shells

Equivalents in every shell

Bashunix
read -p "Name: " name

Bash builtin. `-p` shows a prompt, `-r` disables backslash escapes (almost always wanted), `-s` hides input (passwords), `-t N` sets a timeout.

Zshunix
read "name?Name: "

Zsh syntax for prompt is `"VAR?prompt"` (single quoted-arg form). Bash's `-p` flag also works.

Fishunix
read --prompt-str "Name: " name

Fish uses long-form flags. `--prompt-str` for a static prompt; `--silent` (`-s`) for passwords; `--list` reads into an array variable.

PowerShellwindows
$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.

cmd.exewindows
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

Bash
read -rp "Name: " name && echo "Hello, $name"
Fish
read --prompt-str "Name: " name; echo "Hello, $name"
PowerShell
$name = Read-Host "Name"; "Hello, $name"
cmd.exe
set /p name=Name: && echo Hello, %name%

Read a password without echoing

Bash
read -rsp "Password: " pw && echo
Fish
read --silent --prompt-str "Password: " pw
PowerShell
$pw = Read-Host -Prompt "Password" -AsSecureString

Read each line of a file into a variable

Bash
while IFS= read -r line; do echo "$line"; done < file.txt
Zsh
while IFS= read -r line; do echo "$line"; done < file.txt
PowerShell
Get-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

pwsh`Read-Host` works identically on Windows, Linux, and macOS pwsh — the terminal-input layer abstracts the OS. `-AsSecureString` yields a `SecureString` on every platform, but on Linux/macOS it has no DPAPI backing, so do not persist it across sessions: encrypt with `ConvertFrom-SecureString -Key` first.
WSLWSL bash `read` reads from the Linux pty and behaves identically to native Linux. Piping from a Windows interop binary (e.g. `powershell.exe -c "..." | read line`) often produces CRLF line endings — strip with `read -r line && line=${line%$'\r'}` to avoid silent trailing-carriage-return bugs in `case` matches.

Common tasks using read

Related commands