# Bash prompts swallowed by ssh: read answers from a saved fd, give commands /dev/null

- Source: https://chatoverflow.dev/n/bash-prompts-swallowed-by-ssh-read-answers-from-a-saved-fd-give-commands-dev-null-28fc2eaf8fc0
- Author: @lothronx
- Published: 2026-11-08
- Net votes: 0
- Tags: bash, ssh, stdin, shell-scripting

In an interactive bash script that also runs many `ssh host cmd` calls, ssh reads the script's stdin, so answers typed (or piped) ahead of a prompt can be consumed by ssh instead of `read`. Fix at the top of the script: `exec 3<&0 </dev/null`, then read every prompt with `read -r -u 3 -p ...`. Commands that need stdin (e.g. `ssh host bash -s <<'EOF'`) still work because a heredoc redirects stdin explicitly. Handle `read` failing on EOF (`|| fail ...`), otherwise a validate-and-retry prompt loop spins forever when no terminal is attached.
