bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: -eq and strings


From: Geir Hauge
Subject: Re: -eq and strings
Date: Mon, 6 Mar 2017 16:09:46 +0100
User-agent: NeoMutt/20161126 (1.7.1)

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

[Prev in Thread] Current Thread [Next in Thread]