0
0
Bash Scriptingscripting~20 mins

Arithmetic expansion $(( )) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arithmetic Expansion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of arithmetic expansion with mixed operators
What is the output of this bash command?
echo $((3 + 4 * 2 - 1))
Bash Scripting
echo $((3 + 4 * 2 - 1))
A10
B14
C12
D9
Attempts:
2 left
💡 Hint
Remember the order of operations: multiplication before addition and subtraction.
💻 Command Output
intermediate
2:00remaining
Result of arithmetic expansion with variable substitution
Given x=5, what is the output of this command?
x=5
echo $((x * 3 + 2))
Bash Scripting
x=5
echo $((x * 3 + 2))
A21
B15
C17
D11
Attempts:
2 left
💡 Hint
Substitute x with 5 and calculate.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in arithmetic expansion
Which option contains a syntax error in arithmetic expansion?
Aecho $((3 + ))
Becho $((10 / 2))
Cecho $((7 - 4))
Decho $((3 + 5))
Attempts:
2 left
💡 Hint
Look for incomplete expressions inside $(( )).
🔧 Debug
advanced
2:00remaining
Why does this arithmetic expansion output 0?
Given a=3 and b=5, what is the output of this command and why?
a=3
b=5
echo $((a / b))
Bash Scripting
a=3
b=5
echo $((a / b))
A0 because division by zero returns zero
B0 because b is zero
C0 because variables are not expanded inside $(( ))
D0 because integer division truncates the decimal part
Attempts:
2 left
💡 Hint
Think about how bash handles division with integers.
🚀 Application
expert
3:00remaining
Calculate factorial using arithmetic expansion in a loop
What is the output of this bash script?
n=4
fact=1
for ((i=1; i<=n; i++)); do
  fact=$((fact * i))
done
echo $fact
Bash Scripting
n=4
fact=1
for ((i=1; i<=n; i++)); do
  fact=$((fact * i))
done
echo $fact
A10
B24
C1
D120
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.