How do I test if a variable is a number in bash?

Posted by Flávio Amieiro on Stack Overflow See other posts from Stack Overflow or by Flávio Amieiro
Published on 2009-04-30T13:30:16Z Indexed on 2010/04/24 14:23 UTC
Read the original article Hit count: 294

Filed under:
|
|

I just can't figure out how do I make sure an argument passed to my script is a number or not.

All I want to do is something like this:

test *isnumber* $1 && VAR=$1 || echo "need a number"

Any help?

UPDATE: I managed (whit Charles' help) to do it, but I'm not yet sure it's the best way to do that (even though it worked on my tests). This is how it ended up:

[[ $1 =~ "^[0-9]+$" ]] && echo "numero" && exit 0 || echo "nao numero" && exit 1

© Stack Overflow or respective owner

Related posts about shell

Related posts about bash