How to test if a variable is an integer in BASH?

How do I test if a variable is an integer in BASH?
LVL 2
itniflAsked:
Who is Participating?
 
Maciej SsysadminCommented:
Or (if you don't mind, I'll stick with my version ;)) - to honor negative integers also:
#!/bin/sh
 
VAR=${1}
 
echo "${VAR}" | grep -v -- "^-\?[0-9]\+$" > /dev/null >&1
if [ "$?" -eq "0" ]; then
   echo string
else
   echo integer
fi

Open in new window

0
 
Ken FayalCTOCommented:
This might help to shed light on the situation:

http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Quick_Reference
0
 
itniflAuthor Commented:
That was alot of useful information, but it didnt seem to show how to test if a variabel is a integer.
If I am mistaking, please correct me and paste the text of value in here.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cakuzoCommented:
You can test if a variable is a integer by testing with an arithmetic operator. Look at my example script.
#!/bin/bash
VARIABLE="$1"
 
if [ $VARIABLE -eq $VARIABLE 2> /dev/null ]; then
echo "integer"
else
echo "not integer"
fi

Open in new window

0
 
Maciej SsysadminCommented:
It won't work if you provide " " as parameter to script.
Below should work even then.
#!/bin/sh
 
VAR=${1}
 
echo "${VAR}" | grep "[^0-9]" > /dev/null >&1
if [ "$?" -eq "0" ]; then
   echo string
else
   echo integer
fi

Open in new window

0
 
cakuzoCommented:
Sorry oklit,
my solution works in every case and even for negative integer values. You don't need to grep a regexp like oklit does. Try it!

If you don't want negative integers you can also test with:


if [ $VARIABLE -ge 0 2>/dev/null ]; then

Open in new window

0
 
Maciej SsysadminCommented:
I tried your solution (23699766) before posting :)
Assuming it is in script named "script.sh". Run it with command:

script.sh " "
Your script returns "integer", which is not true.

On the other hand - my script doesn't honor negative integers :)
So - I would rather connect these two scripts to achieve author's needs :)
0
 
cakuzoCommented:
For my first solution I agree with oklit it works not when the variable is null or empty. What I don't like on oklits solution is that it has too much overhead, which is not necessary.

My second solution works great for positive integer values (even if null or empty string is beeing passed)

This solution is the best I can provide for testing negative and positive values!
 
#!/bin/sh
x=$1
 
if (($x)); then
  echo "true"
else
  echo "false"
fi

Open in new window

0
 
cakuzoCommented:
And to suppress eventually stderr message just do it like this
#!/bin/sh
x=$1
 
if (($x)) 2>/dev/null; then
  echo "true"
else
  echo "false"
fi

Open in new window

0
 
itniflAuthor Commented:
Very good! Thanks! I learned alot from this!
0
 
itniflAuthor Commented:
The solutions were interesting and correct. Thanks!
0
 
Maciej SsysadminCommented:
cakuzo: are you completely sure, that your last script is working with every possible input? :) Try passing number 0, or 00, 000, etc... to your script :)
0
 
TintinCommented:
Both your tests fail for +1, which is a valid integer.

Here's a version that caters for positive integers
#!/bin/bash
VAR=$1
 
if echo $VAR | grep -Eq '^[+-]?[0-9]+$'
then
   echo integer
else
   echo non integer
fi

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.