On Mon, Mar 06, 2017 at 03:31:31PM +0100, Rob la Lau wrote:
> On 06-03-17 15:16, Reuti wrote:
> > if ! expr "$x" : '[[:digit:]]*$' >/dev/null; then echo no; fi
> > 
> > if [ -n "${x//[0-9]/}" ]; then echo no; fi
> 
> True, but regular expressions are usually considered expensive.
> (Must admit I never benchmarked.)
> 
> And I know 1 regex won't considerably slow down my script, but it's
> become a habit to always try and program as light as possible.

If you're counting milliseconds, pattern matching should be slightly
faster than your -eq hack.

is_digit() { [[ -n $1 && $1 != *[![:digit:]]* ]]; }

or the sh variant:

is_digit() { case $1 in ''|*[![:digit:]]*) return 1;; esac; }


See also http://mywiki.wooledge.org/BashFAQ/054

-- 
Geir Hauge

Reply via email to