What is the purpose of 'declare -i' in bash scripting?

QuestionsQuestions8 SkillsProLinux Variable DeclaringSep, 03 2025
0155

The declare -i command in bash scripting is used to declare a variable as an integer. This allows the variable to be treated as a numeric value, enabling arithmetic operations to be performed on it.

For example:

declare -i number=5
declare -i result=number*2
echo $result  # This will output 10

In this example, number is declared as an integer, and when multiplied by 2, the result is calculated correctly as an integer. This is particularly useful for counters, calculations, and any numeric processing in your scripts.

0 Comments

no data
Be the first to share your comment!