Bird
0
0

What is the output of this script?

medium📝 Command Output Q4 of 15
Bash Scripting - Loops
What is the output of this script?
for num in 1 2 3; do echo $((num * 2)); done
A2 4 6
B2\n4\n6
Cnum * 2 num * 2 num * 2
DError: invalid arithmetic
Step-by-Step Solution
Solution:
  1. Step 1: Understand arithmetic expansion

    $((num * 2)) calculates num times 2 for each loop iteration.
  2. Step 2: Determine output format

    echo prints each result on a new line, so output is 2, then 4, then 6 each on its own line.
  3. Final Answer:

    2\n4\n6 -> Option B
  4. Quick Check:

    Arithmetic expansion outputs doubled numbers line by line [OK]
Quick Trick: Use $(( )) for arithmetic inside loops [OK]
Common Mistakes:
MISTAKES
  • Expecting all numbers on one line
  • Not using $(( )) for math
  • Thinking it prints literal expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes