Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3149
  • Last Modified:

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

How do I test if a variable is an integer in BASH?
0
itnifl
Asked:
itnifl
  • 4
  • 4
  • 3
  • +2
2 Solutions
 
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
 
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
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
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now