Bird
0
0

Which of the following correctly uses arithmetic expansion to subtract 7 from 15 in bash?

easy🧠 Conceptual Q2 of 15
Bash Scripting - Quoting and Expansion
Which of the following correctly uses arithmetic expansion to subtract 7 from 15 in bash?
Aresult=$[15 - 7]
Bresult=$(15 - 7)
Cresult=$((15 - 7))
Dresult=15 - 7
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct arithmetic syntax

    Arithmetic expansion requires $(( expression )) syntax.
  2. Step 2: Check each option

    result=$((15 - 7)) uses correct syntax. result=$(15 - 7) uses command substitution which treats it as a command, not math. result=$[15 - 7] uses deprecated syntax. result=15 - 7 assigns a string, not calculation.
  3. Final Answer:

    result=$((15 - 7)) -> Option C
  4. Quick Check:

    Correct syntax = result=$((15 - 7)) [OK]
Quick Trick: Always use $(( )) for math, not $() or plain assignment [OK]
Common Mistakes:
MISTAKES
  • Confusing $() with $(( ))
  • Using deprecated $[] syntax
  • Assigning math as string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes