Bird
0
0

Why does the read -p command not print the prompt text when used inside a script redirected from a file (e.g., bash script.sh < input.txt)?

hard🧠 Conceptual Q10 of 15
Bash Scripting - User Input
Why does the read -p command not print the prompt text when used inside a script redirected from a file (e.g., bash script.sh < input.txt)?
ABecause <code>read</code> only works interactively
BBecause <code>read</code> detects input is not from terminal and skips prompt
CBecause input redirection disables all output
DBecause <code>-p</code> option is ignored in scripts
Step-by-Step Solution
Solution:
  1. Step 1: Understand input redirection effect

    When input is redirected, read input is not from terminal.
  2. Step 2: Behavior of read -p

    read shows prompt only if input is from terminal; otherwise, prompt is skipped.
  3. Final Answer:

    Because read detects input is not from terminal and skips prompt -> Option B
  4. Quick Check:

    Prompt shown only if input is terminal = D [OK]
Quick Trick: Prompt appears only if input is from terminal [OK]
Common Mistakes:
MISTAKES
  • Thinking -p is ignored always
  • Assuming input redirection disables all output
  • Believing read only works interactively

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes