Bird
0
0

Which of the following is the correct syntax to add two variables a and b using arithmetic expansion in bash?

easy📝 Syntax Q12 of 15
Bash Scripting - Quoting and Expansion
Which of the following is the correct syntax to add two variables a and b using arithmetic expansion in bash?
Asum=$((a + b))
Bsum=$[a + b]
Csum=$(a + b)
Dsum=$((a, b))
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct arithmetic expansion syntax

    In bash, arithmetic expansion uses $((expression)) to evaluate math expressions.
  2. Step 2: Check each option

    sum=$((a + b)) uses $((a + b)), which is the correct syntax to add variables a and b.
  3. Final Answer:

    sum=$((a + b)) -> Option A
  4. Quick Check:

    Use $(( )) for math with variables [OK]
Quick Trick: Use $(( )) for math, not $[] or $() [OK]
Common Mistakes:
MISTAKES
  • Using $[] which is deprecated
  • Using $() which is command substitution
  • Using commas inside arithmetic expansion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes