12.3. Arithmetic
12.3.1. Integers (declare and let Commands)
The declare Command. Variables can be declared as integers with the declare –i command. If you attempt to assign any string value, bash assigns 0 to the variable. Arithmetic can be performed on variables that have been declared as integers. (If the variable has not been declared as an integer, the built-in let command allows arithmetic operations. See “The let Command” on page 732.) If you attempt to assign a floating point number, bash reports a syntax error. Numbers can also be represented in different bases such as binary, octal, and hex.
Example 12.6.
1 $ declare –i num 2 $ num=hello $ echo $num 0 3 $ num=5 + 5 bash: +: command not found 4 $ num=5+5 $ echo $num 10 5 $ num=4*6 ... |
Get UNIX® Shells by Example, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.