Shell script linux, validating integer

Gerardo agustin Riera

This code is for check if a character is a integer or not (i think). I'm trying to understand what this means, I mean... each part of that line, checking the GREP man pages, but it's really difficult for me. I found it on the internet. If anyone could explain me the part of the grep... what means each thing put there:

echo $character | grep -Eq '^(\+|-)?[0-9]+$'

Thanks people!!!

anubhava

Analyse this regex:

'^(\+|-)?[0-9]+$'

^ - Line Start
(\+|-)? - Optional + or - sign at start
[0-9]+ - One or more digits
$ - Line End

Overall it matches strings like +123 or -98765 or just 9

Here -E is for extended regex support and -q is for quiet in grep command.

PS: btw you don't need grep for this check and can do this directly in pure bash:

re='^(\+|-)?[0-9]+$'
[[ "$character" =~ $re ]] && echo "its an integer"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related