Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Integer variables in Bash Scripting - Mini Project: Build & Apply

Choose your learning style10 modes available
Integer variables
📖 Scenario: You are writing a simple bash script to store and use integer numbers. This is like keeping track of counts or scores in a game.
🎯 Goal: Learn how to create integer variables in bash, assign values, and display them.
📋 What You'll Learn
Create integer variables with exact names and values
Use bash syntax for integer assignment
Print the integer variable values correctly
💡 Why This Matters
🌍 Real World
Integer variables are used in scripts to count items, track progress, or calculate values automatically.
💼 Career
Knowing how to handle numbers in bash scripts is important for automation tasks in system administration and DevOps.
Progress0 / 4 steps
1
Create integer variables
Create two integer variables called count and max. Set count to 5 and max to 10 using bash integer assignment syntax.
Bash Scripting
Hint

Use variable=value without spaces for integer assignment in bash.

2
Add a limit variable
Add a new integer variable called limit and set it to 15.
Bash Scripting
Hint

Remember no spaces around the equal sign.

3
Calculate total
Create a new integer variable called total that adds count and max using bash arithmetic expansion.
Bash Scripting
Hint

Use $((expression)) to do arithmetic in bash.

4
Print the total
Print the value of the total variable using echo.
Bash Scripting
Hint

Use echo $total to display the value.

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