Simulate User Input to an Interactive Prompt in a Test
Answer an interactive prompt in a test with a here-string, a pipe or yes. The prompt text from read -p is not printed when input is not a terminal.
A script asks a question with read, and you want to test both answers without typing.
cat > ask.sh <<'EOF'
#!/bin/bash
read -rp "Continue? [y/N] " ans
case $ans in
y | Y) echo "continuing" ;;
*) echo "aborted" ;;
esac
EOF
bash ask.sh <<< y
bash ask.sh <<< n
printf 'y\n' | bash ask.sh
bash ask.sh < /dev/null
yes | bash ask.shcontinuing
aborted
continuing
aborted
continuingbash ask.sh <<< y feeds the answer on standard input, and a printf pipe or yes | do the same. With < /dev/null the read hits end of file, returns failure and leaves the variable empty, which lands in the default branch and printed aborted. The run output has no Continue? text. The manual says that read -p displays the prompt only if input comes from a terminal, so tests also hide the prompt. See the read builtin.
Scripts that read from /dev/tty directly ignore redirected input, and password prompts using read -s or sudo do the same. For those use expect or a pseudo-terminal such as script. A here-string appends a newline, which is what read needs.