[[ $var =~ ^-?[0-9]+$ ]]
^
indicates the beginning of the input pattern-
is a literal "-"?
means "0 or 1 of the preceding (-
)"+
means "1 or more of the preceding ([0-9]
)"$
indicates the end of the input patternSo the regex matches an optional -
(for the case of negative numbers), followed by one or more decimal digits.
References: