[[ $var =~ ^-?[0-9]+$ ]]
- The
^
indicates the beginning of the input pattern - The
-
is a literal “-“ - The
?
means “0 or 1 of the preceding (-
)” - The
+
means “1 or more of the preceding ([0-9]
)” - The
$
indicates the end of the input pattern
So the regex matches an optional -
(for the case of negative numbers), followed by one or more decimal digits.
References:
- http://www.tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF