Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Integer variables 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 declare an integer variable named num with value 10.

Bash Scripting
declare -i [1]=10
Drag options to blanks, or click blank then click option'
Anum
Bnumber
Cint
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from 'num'.
Forgetting to declare the variable as integer.
2fill in blank
medium

Complete the code to add 5 to the integer variable count.

Bash Scripting
count=[1]+5
Drag options to blanks, or click blank then click option'
Acount
B$count
C"count"
D'count'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without $.
Using quotes around the variable name.
3fill in blank
hard

Fix the error in the code to correctly increment the integer variable score by 1.

Bash Scripting
(( [1]++ ))
Drag options to blanks, or click blank then click option'
A'score'
B$score
C"score"
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using $score inside arithmetic expression.
Using quotes around the variable name.
4fill in blank
hard

Fill in the blanks to declare an integer variable total and assign it the sum of a and b.

Bash Scripting
declare -i [1]=[2]+[3]
Drag options to blanks, or click blank then click option'
Atotal
Ba
Cb
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name for declaration.
Using the same variable twice instead of both a and b.
5fill in blank
hard

Fill all three blanks to create a loop that counts from 1 to 5 and prints each number.

Bash Scripting
for [1] in [2]; do
  echo [3]
done
Drag options to blanks, or click blank then click option'
Ai
B1 2 3 4 5
C$i
Dseq 1 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without $ in echo.
Using seq 1 5 without command substitution.
Using quotes around the list of numbers.

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