[olug] Test for integer in bash

Obi-Wan obiwan at jedi.com
Wed Nov 28 22:46:00 UTC 2007


>   echo "$1" | grep ^[0-9]*$ > /dev/null
>
> testInt
> 
> Secondly, I'm sure you've noticed that the last test of nothing
> returns as a match.  Why can't I get the correct regex,  echo 900 |
> grep ^[0-9]+$  , to work?  echo 900 | grep ^[0-9]{1,}$ fails too.

In your last example, you're trying to use perl regex, which doesn't
work in grep (or even egrep).  What you want is "grep "^[0-9][0-9]*$".

-- 
Ben "Obi-Wan" Hollingsworth                             obiwan at jedi.com
   The stuff of earth competes for the allegiance I owe only to the
     Giver of all good things, so if I stand, let me stand on the
       promise that You will pull me through.  -- Rich Mullins



More information about the OLUG mailing list