0
0
Bash Scriptingscripting~10 mins

Integer variables in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer variables
Declare integer variable
Assign integer value
Use variable in expressions
Print or manipulate value
End
This flow shows how to declare an integer variable, assign a value, use it in calculations, and print the result.
Execution Sample
Bash Scripting
count=5
count=$((count + 3))
echo $count
This script sets an integer variable, adds 3 to it, and prints the new value.
Execution Table
StepActionVariableValueOutput
1Declare variable count with value 5count5
2Add 3 to count using arithmetic expansioncount8
3Print value of countcount88
4End of script
💡 Script ends after printing the updated integer variable value.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
countundefined588
Key Moments - 2 Insights
Why do we use $(( )) around the expression when updating the variable?
The $(( )) tells bash to treat the expression inside as an arithmetic operation. Without it, bash treats it as a string. See Step 2 in the execution_table where count is updated.
What happens if we try to assign a non-integer value to count?
Bash will treat it as a string, and arithmetic operations may fail or give unexpected results. Integer variables in bash are just variables used with arithmetic expansion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count after Step 1?
A5
B8
Cundefined
D3
💡 Hint
Check the 'Value' column for count at Step 1 in the execution_table.
At which step does the variable count change from 5 to 8?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Value' columns in the execution_table to see when count updates.
If we remove $(( )) in Step 2, what would happen to count?
Acount becomes 3
Bcount becomes 8
Ccount becomes the string '5 + 3'
DScript will error out
💡 Hint
Recall that $(( )) is needed for arithmetic; without it, bash treats the right side as a string.
Concept Snapshot
Integer variables in bash are normal variables used with arithmetic expansion.
Use count=5 to assign.
Update with count=$((count + 3)).
Print with echo $count.
$(( )) tells bash to calculate the expression.
Full Transcript
In bash scripting, integer variables are just variables that hold numbers. You assign them like count=5. To do math, you use $(( )) around the expression, like count=$((count + 3)). This tells bash to add 3 to count. Then you can print the value with echo $count. Without $(( )), bash treats the right side as text, not a number. This example shows declaring count as 5, adding 3, and printing 8.