Introduction
Integer variables store whole numbers so you can do math and count things in your script.
declare -i variable_name=number
# or simply
variable_name=numberdeclare -i count=10number=5declare -i total total=7 total=total+3
#!/bin/bash # Declare integer variable declare -i score=0 # Add points score=score+10 score=score+5 # Print the score echo "Your score is $score"
declare -i num=5 do in a bash script?declare -i command in bash makes a variable hold integer values only.num is assigned the integer value 5, so it becomes an integer variable.count with initial value 10 in bash?declare -i is used to declare integer variables.declare -i. int count=10 is not valid bash syntax.declare -i x=7 declare -i y=3 x=$x+$y echo $x
x and y are declared as integers with values 7 and 3.x=$x+$yx=7+3. Since x is declare -i, bash performs arithmetic evaluation on the RHS: 7 + 3 = 10.declare -i total=5 total=total+3 echo $total
total is declared as integer with value 5 [check].total=total+3declare -i, RHS total+3 is arithmetic context. Bare total expands to 5, evaluates 5+3=8.i. Which snippet correctly increments i inside a loop?i integer=1, loop while <=5, echo i, increment properly to print 1-5.i=i +1 parses as i=i (no change) then invalid +1 command, fails.i++ invalid command without (( )).-i causes string concat 1+1->11 etc.i=$((i+1)) explicit arithmetic works portably.Pass the test and this pattern moves unknown