10.2-RELEASE/amd64: grep regex syntax vs. grep-bug

RW rwmaillists at googlemail.com
Mon Sep 28 13:07:56 UTC 2015


On Mon, 28 Sep 2015 13:37:35 +0200
no at spam@mgEDV.net wrote:

> hi folks,
> the goal: grep, that a variable contains a number and JUST digits.
> # setup sample variable - we're on /bin/sh @ 10.2-RELEASE/amd64.
> x=""
> # od output the content of the variable to ensure content
> echo "$x" | od -ctdC
> 0000000   \n
>            10
> 0000001
> # now try /usr/bin/grep'pin (C locale) that it contains...
> # ^          at BOL...
> # [0-9]   require a digit
> # *          ... and digits following

You're confusing + and *.

[0-9]*  matches any number of digits including zero

[0-9]+  matches one or more digits.


> # $          ...until we reach EOL
> # ... but...:
> echo "$x" | grep -c '^[0-9]*$'
> 1             <== WHY?



More information about the freebsd-questions mailing list