Division by zero is not allowed and causes a runtime error in bash arithmetic expansion.
Final Answer:
Division by zero error causes runtime failure -> Option D
Quick Check:
Division by zero is invalid in $(( )) [OK]
Hint: Never divide by zero inside $(( )) [OK]
Common Mistakes:
Thinking it's a syntax error
Assuming division by zero returns zero
Believing arithmetic expansion disallows division
5. You want to calculate the factorial of a number stored in variable n using a bash loop and arithmetic expansion. Which snippet correctly updates the factorial value inside the loop?
hard
A. fact=$[fact * i]
B. fact=$(fact * i)
C. fact=$((fact * i))
D. fact=$((fact + i))
Solution
Step 1: Understand factorial calculation logic
Factorial multiplies all integers from 1 to n, so fact must be multiplied by i each loop.
Step 2: Identify correct arithmetic expansion syntax for multiplication
fact=$((fact * i)) uses $((fact * i)), which correctly multiplies fact by i and updates fact.
Final Answer:
fact=$((fact * i)) -> Option C
Quick Check:
Use $(( )) for multiplication updates in loops [OK]
Hint: Use $((fact * i)) to multiply and update variable [OK]
Common Mistakes:
Using $() which runs commands, not math
Using $[] which is deprecated
Adding instead of multiplying for factorial
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.