Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Arithmetic expansion $(( )) in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sum of 5 and 3 using arithmetic expansion.

Bash Scripting
result=$((5 [1] 3))
echo $result
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' will subtract instead of add.
Using '*' or '/' will multiply or divide, not add.
2fill in blank
medium

Complete the code to multiply 7 by 4 using arithmetic expansion.

Bash Scripting
result=$((7 [1] 4))
echo $result
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' will add or subtract instead of multiply.
Using '/' will divide instead of multiply.
3fill in blank
hard

Fix the error in the code to correctly subtract 2 from 10 using arithmetic expansion.

Bash Scripting
result=$((10 [1] 2))
echo $result
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' will add instead of subtract.
Using '*' or '/' will multiply or divide instead of subtract.
4fill in blank
hard

Fill both blanks to calculate (17 % 5) - 2 using arithmetic expansion.

Bash Scripting
result=$((17 [1] 5 [2] 2))
echo $result
Drag options to blanks, or click blank then click option'
A%
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' changes the result.
Using '*' instead of '%' does not give remainder.
5fill in blank
hard

Fill all three blanks to create a dictionary-like output with keys as numbers and values as their squares, but only for numbers greater than 3.

Bash Scripting
declare -A squares
for num in {1..5}; do
  if (( num [1] 3 )); then
    squares[[2]]=$((num [3] num))
  fi
done
for key in "${!squares[@]}"; do
  echo "$key: ${squares[$key]}"
done
Drag options to blanks, or click blank then click option'
A>
Bnum
C*
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes the filter condition.
Using wrong variable names causes errors.
Using '+' instead of '*' does not square the number.

Practice

(1/5)
1. What does the expression $((3 + 4)) do in a bash script?
easy
A. Concatenates 3 and 4 as strings to get 34
B. Calculates the sum of 3 and 4 and returns 7
C. Throws a syntax error because of missing quotes
D. Assigns 3 to variable 4

Solution

  1. Step 1: Understand arithmetic expansion syntax

    The $(( )) syntax evaluates the expression inside as an arithmetic operation.
  2. Step 2: Calculate the expression inside

    Inside the parentheses, 3 + 4 equals 7.
  3. Final Answer:

    Calculates the sum of 3 and 4 and returns 7 -> Option B
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Remember $(( )) does math, not string join [OK]
Common Mistakes:
  • Thinking it concatenates numbers as strings
  • Forgetting to use $(( )) for arithmetic
  • Expecting quotes around the expression
2. Which of the following is the correct syntax to add two variables a and b using arithmetic expansion in bash?
easy
A. sum=$((a + b))
B. sum=$[a + b]
C. sum=$(a + b)
D. sum=$((a, b))

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]
Hint: Use $(( )) for math, not $[] or $() [OK]
Common Mistakes:
  • Using $[] which is deprecated
  • Using $() which is command substitution
  • Using commas inside arithmetic expansion
3. What will be the output of the following bash script?
#!/bin/bash
x=5
y=3
result=$((x * y + 2))
echo $result
medium
A. 17
B. 21
C. 15 + 2
D. Syntax error

Solution

  1. Step 1: Substitute variable values in the expression

    x=5 and y=3, so expression is 5 * 3 + 2.
  2. Step 2: Calculate the arithmetic expression

    5 * 3 = 15, then 15 + 2 = 17.
  3. Final Answer:

    17 -> Option A
  4. Quick Check:

    5 * 3 + 2 = 17 [OK]
Hint: Calculate multiplication before addition inside $(( )) [OK]
Common Mistakes:
  • Treating expression as string concatenation
  • Ignoring operator precedence
  • Expecting syntax error due to variables
4. Identify the error in this bash snippet:
#!/bin/bash
num=10
result=$((num / 0))
echo $result
medium
A. Arithmetic expansion does not support division
B. Syntax error due to missing spaces
C. Variable num is not defined
D. Division by zero error causes runtime failure

Solution

  1. Step 1: Analyze the arithmetic expression

    The expression attempts to divide num (10) by 0.
  2. Step 2: Understand division by zero in bash

    Division by zero is not allowed and causes a runtime error in bash arithmetic expansion.
  3. Final Answer:

    Division by zero error causes runtime failure -> Option D
  4. 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

  1. Step 1: Understand factorial calculation logic

    Factorial multiplies all integers from 1 to n, so fact must be multiplied by i each loop.
  2. Step 2: Identify correct arithmetic expansion syntax for multiplication

    fact=$((fact * i)) uses $((fact * i)), which correctly multiplies fact by i and updates fact.
  3. Final Answer:

    fact=$((fact * i)) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard