Forrest logo
back to the declare tool

declare:tldr:d329c

declare: Declare an integer variable with the specified value.
$ declare -i ${variable}="${value}"
try on your machine

The declare -i command in Bash is used to declare a variable as an integer. The ${variable} represents the name of the variable you want to declare, and "${value}" represents the value that you want to assign to the variable.

With the -i option, Bash treats the variable as an integer and performs arithmetic operations on it. If the assigned value is not a valid integer, it will be treated as zero. For example, if you set value="abc", the variable will be initialized to 0.

Here's an example to illustrate its usage:

declare -i number=5
echo $number   # Output: 5

number="abc"
echo $number   # Output: 0

number=10/2
echo $number   # Output: 5, as 10 divided by 2 is equal to 5

In the above example, the variable number is declared as an integer using declare -i command. The first assignment sets its value to 5. When we assign a non-numeric value "abc" to it, it is treated as 0. Lastly, we assign 10/2 to the variable, and it performs the arithmetic operation which evaluates to 5.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the declare tool