Test whether string is a valid integer

[[ $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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)