Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Integer variables in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Integer Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of integer arithmetic in Bash
What is the output of this Bash script?
Bash Scripting
a=5
b=3
c=$((a + b * 2))
echo $c
A11
B16
C13
D8
Attempts:
2 left
💡 Hint
Remember that multiplication has higher precedence than addition.
📝 Syntax
intermediate
2:00remaining
Correct integer assignment in Bash
Which option correctly assigns the integer value 10 to variable x in Bash?
Ax=10
Bx := 10
Cx == 10
Dlet x = 10
Attempts:
2 left
💡 Hint
In Bash, assignment does not use spaces around = and no extra symbols.
🔧 Debug
advanced
2:00remaining
Why does this Bash script fail to add integers?
Given this script: x=5 y=3 z=$x + $y echo $z What is the output and why?
Bash Scripting
x=5
y=3
z="$x + $y"
echo $z
A53
B8
CSyntax error
D5 + 3
Attempts:
2 left
💡 Hint
Check how arithmetic is done in Bash and how variables are expanded.
🚀 Application
advanced
3:00remaining
Calculate factorial using integer variables in Bash
Which script correctly calculates factorial of 4 and prints the result?
A
n=4
fact=1
while [ $n -gt 0 ]; do
  fact=$((fact * n))
  n=$((n - 1))
done
echo $fact
B
n=4
fact=1
for ((i=1; i<=n; i++)); do
  fact=$((fact * i))
done
echo $fact
C
n=4
fact=1
for i in $(seq 1 $n); do
  fact=$((fact + i))
done
echo $fact
D
n=4
fact=1
for i in {1..n}; do
  fact=$((fact * i))
done
echo $fact
Attempts:
2 left
💡 Hint
Check the loop syntax and arithmetic operation used for factorial.
🧠 Conceptual
expert
2:00remaining
Integer variable behavior in Bash with arithmetic expansion
Consider this Bash snippet: x=10 y=3 z=$((x / y)) echo $z What is the output and why?
A3.3333 because Bash supports floating point division
B4 because Bash rounds up the division result
C3 because Bash integer division truncates the decimal part
DError because division is not supported in Bash arithmetic
Attempts:
2 left
💡 Hint
Bash arithmetic expansion works with integers only.

Practice

(1/5)
1. What does the command declare -i num=5 do in a bash script?
easy
A. It creates an integer variable named num with value 5.
B. It creates a string variable named num with value '5'.
C. It declares a function named num that returns 5.
D. It prints the number 5 to the terminal.

Solution

  1. Step 1: Understand declare -i usage

    The declare -i command in bash makes a variable hold integer values only.
  2. Step 2: Analyze the assignment

    Here, num is assigned the integer value 5, so it becomes an integer variable.
  3. Final Answer:

    It creates an integer variable named num with value 5. -> Option A
  4. Quick Check:

    declare -i num=5 means integer variable num = 5 [OK]
Hint: declare -i means integer variable assignment [OK]
Common Mistakes:
  • Thinking declare -i creates a string variable
  • Confusing declare -i with printing output
  • Assuming declare -i creates a function
2. Which of the following is the correct way to declare an integer variable count with initial value 10 in bash?
easy
A. count=10
B. declare count=10
C. declare -i count=10
D. int count=10

Solution

  1. Step 1: Recall integer declaration syntax

    In bash, declare -i is used to declare integer variables.
  2. Step 2: Check each option

    count=10 assigns a value but does not declare integer type. declare count=10 is invalid syntax. declare -i count=10 correctly uses declare -i. int count=10 is not valid bash syntax.
  3. Final Answer:

    declare -i count=10 -> Option C
  4. Quick Check:

    declare -i count=10 is correct integer declaration [OK]
Hint: Use declare -i to declare integer variables [OK]
Common Mistakes:
  • Using declare without -i for integers
  • Trying C-style int declaration in bash
  • Assigning without declare -i loses integer behavior
3. What is the output of this bash script snippet?
declare -i x=7
declare -i y=3
x=$x+$y
echo $x
medium
A. 10
B. 7+3
C. 73
D. Error

Solution

  1. Step 1: Understand variable declarations

    Both x and y are declared as integers with values 7 and 3.
  2. Step 2: Analyze the assignment x=$x+$y

    Parameter expansion produces x=7+3. Since x is declare -i, bash performs arithmetic evaluation on the RHS: 7 + 3 = 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    $x+$y expands to 7+3, -i evaluates to 10 [OK]
Hint: declare -i evaluates valid arithmetic RHS like num+num [OK]
Common Mistakes:
  • Thinking it concatenates to 73
  • Expecting literal '7+3' output
  • Believing explicit $(( )) always required
4. What is the output of this bash script snippet?
declare -i total=5
total=total+3
echo $total
medium
A. 5
B. 8
C. total+3
D. Error

Solution

  1. Step 1: Check variable declaration

    total is declared as integer with value 5 [check].
  2. Step 2: Analyze assignment total=total+3

    With declare -i, RHS total+3 is arithmetic context. Bare total expands to 5, evaluates 5+3=8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    Bare var in -i RHS expands + computes math [OK]
Hint: Bare variable names expand in -i arithmetic assignments [OK]
Common Mistakes:
  • Assuming literal 'total+3' without $
  • Thinking no math without $(( ))
  • Expecting unchanged 5
5. You want to create a bash script that counts from 1 to 5 using an integer variable i. Which snippet correctly increments i inside a loop?
hard
A. declare -i i=1 while [ $i -le 5 ]; do echo $i i=i +1 done
B. declare -i i=1 while [ $i -le 5 ]; do echo $i i++ done
C. declare i=1 while [ $i -le 5 ]; do echo $i i=$i+1 done
D. declare -i i=1 while [ $i -le 5 ]; do echo $i i=$((i+1)) done

Solution

  1. Step 1: Understand integer variable and loop

    Declare i integer=1, loop while <=5, echo i, increment properly to print 1-5.
  2. Step 2: Check increment methods

    i=i +1 parses as i=i (no change) then invalid +1 command, fails.
    i++ invalid command without (( )).
    No -i causes string concat 1+1->11 etc.
    i=$((i+1)) explicit arithmetic works portably.
  3. Final Answer:

    i=$((i+1)) -> Option D
  4. Quick Check:

    $((i+1)) for safe integer increment [OK]
Hint: Use i=$((i+1)) or ((i++)) to increment integers [OK]
Common Mistakes:
  • Spaces in i=i +1 cause assignment split + command error
  • i++ without (( )) is invalid command
  • No -i leads to string concatenation
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