Check if variable is a number

(($1 > 0)) && echo "var is a number"
calculate if "$1" is a number ... decimals included :)

By: khayyam
2013-03-22 01:15:00

3 Alternatives + Submit Alt

What Others Think

This code is really helpful to check weather a variable is a number or not and I don't know whether anyone has tried out as none of them has shared the review regarding the code provided so I thought of trying out and will definitely share my review here Hostsailor
Juvanta · 86 weeks and 4 days ago
This successfully validates base-10 integers and hexadecimal numbers, e.g. 0x4A. It fails for numbers such as 345.56, and 1e4, and doesn't handle the error gracefully.
CJK · 39 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: