Bird
0
0

Why does the expression $(( 5 / 2 )) in bash return 2 instead of 2.5?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Quoting and Expansion
Why does the expression $(( 5 / 2 )) in bash return 2 instead of 2.5?
ABash uses floating point but echo truncates output
BDivision operator rounds up automatically
CThe expression is treated as a string, not math
DBash arithmetic expansion only supports integer math, truncating decimals
Step-by-Step Solution
Solution:
  1. Step 1: Understand bash arithmetic limitations

    Bash arithmetic expansion supports only integer calculations.
  2. Step 2: Explain the result

    Division truncates the decimal part, so 5 / 2 equals 2, not 2.5.
  3. Final Answer:

    Bash arithmetic expansion only supports integer math, truncating decimals -> Option D
  4. Quick Check:

    Integer-only math = Bash arithmetic expansion only supports integer math, truncating decimals [OK]
Quick Trick: Bash math truncates decimals, no float support in $(( )) [OK]
Common Mistakes:
MISTAKES
  • Expecting floating point results
  • Using echo to format numbers
  • Confusing string and arithmetic expansion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes