Bird
0
0

What will be the output of this script?

medium📝 Command Output Q5 of 15
Bash Scripting - Quoting and Expansion
What will be the output of this script?
files=$(ls /nonexistent_directory)
echo "$files"
ALists files in /nonexistent_directory
BPrints an empty line
CPrints an error message
DThrows a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand command substitution with error

    ls on a nonexistent directory outputs an error to stderr, not stdout.
  2. Step 2: Assign stdout to variable

    Since no stdout output, variable files is empty.
  3. Step 3: Echo the variable

    echo "$files" prints an empty line.
  4. Final Answer:

    Prints an empty line -> Option B
  5. Quick Check:

    Command error goes to stderr; variable empty = Prints empty line [OK]
Quick Trick: Command errors go to stderr, not captured by $() [OK]
Common Mistakes:
MISTAKES
  • Expecting error message in variable
  • Confusing stdout and stderr
  • Thinking syntax error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes