Numbers and Arithmetic in bash: Difference between revisions
Jump to navigation
Jump to search
Line 46: | Line 46: | ||
local i=0 | local i=0 | ||
((i++)) | ((i++)) | ||
</pre> | |||
=Decrement an Integer= | |||
<pre> | |||
local i=1 | |||
((i--)) | |||
</pre> | </pre> |
Revision as of 22:14, 18 April 2017
Internal
How to Tell if a Variable has an Integer Value
v=10 if expr ${v} + 1 >/dev/null 2>&1; then # # the value contained by v is an integer # else # # the value contained by v is NOT an integer # fi
More about
Floating Point Operations
Multiplication:
local operand1=10.1 local operand2=20.2 result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1*$2}') || exit 1
Division:
local operand1=10.1 local operand2=20.2 result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1/$2}') || exit 1
Increment an Integer
local i=0 ((i++))
Decrement an Integer
local i=1 ((i--))