Skip to contentSkip to conversation

Pattern matching faster than math

DISCUSSION Alex Waite 1 participant 1 message Mar 25, 2015

lore
Share & tools
lore

Alex Waite

author
#0 lore raw

Hello,

This isn't a problem per-se, but I'm curious if anyone can shed some light on why this is so.

I have a script where I'm checking if the contents of a variable is an integer. An easy/hacky way to do this is

[ "$var" -ge 0 2> /dev/null ] || echo "is not int"

But this caused posh to segfault, so I went for a pattern matching solution instead:

[ -z "${var##*[!0-9]*}" ] && echo "is not int"

This works well, and it makes posh happy. But what's surprising to me is that it's faster.

I have more of a write-up in a commit message: https://github.com/zfsnap/zfsnap/commit/ed6326f0006ed18b7d8bb79d5ee8f06142847f41

Any thoughts or insight? Am I making some faulty assumption here?

Thanks for your time.

---Alex