Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for
Did you mean:

SOLVED
Go to solution

## What is an integer?

Hi all,

can anyone tell me the true definition of an integer? My understanding is that is a whole number derived from a mathematical sum - info I found of the web:

"An integer is what is more commonly known as a whole number. It may be positive, negative, or the number zero, but it must be whole. In some cases the definition of whole number will exclude the number zero, or even the set of negative numbers, but this is not as common as the more inclusive use of the term. Integers are the numbers people are most familiar with, and they serve a crucial role in virtually all mathematics."

The reason I ask this question is because I want to know what is the correct syntax when using a test statement ie:

is set MyVar=0 and to test this I would:

if [ \${MyVar} = "0" ]; the
true
etc etc

however if I created a sum or a count:

count=0

while [ \${count} -lt 10 ]; do
etc etc
(( count =+ ))

I understand that using "= != < >" is for testing a string so substituting MyVar=0 does "0" become a string.

Thanks

Chris
hello
6 REPLIES
Honored Contributor

## Re: What is an integer?

simply put an integer is a whole number, including zero. are you asking it in relation to a shell? what shell do you use?

if you have difficulties with expressions, consult the man page:

# man test

or

# man ksh

can also help you, search for the keywords "while", "for" or "if". If you are using other shell look at its man page.
****
Unix operates with beer.
Honored Contributor

## Re: What is an integer?

> substituting MyVar=0 does "0" become a string.

that's because the shell is auto-converting the value.
****
Unix operates with beer.
Acclaimed Contributor
Solution

## Re: What is an integer?

Hi Chris:

> I understand that using "= != < >" is for testing a string so substituting MyVar=0 does "0" become a string.

Yes, I believe so.

In the shell, you can declare an integer (as opposed to a string) with the 'typeset -i' declaration. This speeds up arithmetic operations.

In 'awk' programs you will sometimes see someone do:

# awk '{\$1+0 ...

...which prevents you from doing silly arithmetic with non-numeric data. Compare these results:

# echo FF | awk '{SUM=(\$0+0);print SUM}'

# echo FF | awk '{SUM=(\$0);print SUM}'

Regards!

...JRF...

## Re: What is an integer?

Thanks both for the quick response

As I thought but wanted the experts to confirm

8-)

root:/scratch #echo FF | awk '{SUM=\$0+0);print SUM}'
0
root:/scratch #echo FF | awk '{SUM=(\$0);print SUM}'
FF
hello
Acclaimed Contributor

## Re: What is an integer?

>can anyone tell me the true definition of an integer?

In the shell, an integer is something that's not a string. (And not a floating point number for shells that support those types.)

>if [ \${MyVar} = "0" ]; then

This is a string comparison. If you know MyVar is an integer or numeric you should do:
if [ \$MyVar -eq 0 ]; then
Or even:
if (( Myvar == 0 )); then

>while [ \${count} -lt 10 ]; do

Or:
while (( count < 10 )); do
Highlighted
Honored Contributor

## Re: What is an integer?

use typeset

typeset -i MyVar=0

if (( MyVar = 0 )); then
true
etc etc

typeset -i count=0

while (( count < 10 )); do
etc etc
(( count =+ ))