Skip to main content
added 59 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

POSIXly:

case $1 in
  ("" | *[!0123456789]*)
    echo >&2 Sorry, decimal integer only
    exit 1
esac

Do not use [0-9] which often matches a lot more than [0123456789] (especially with bash globs, YMMV for bash's [[ =~ ]] operator which uses the system regexps where [0-9] may or may not match more than 0123456789 depending on the system and locale).

More on that at what is the meaning of this shell script function

Note that you may also want to reject numbers like 08 or 09 which some tools (including bash arithmetic operators) reject as invalid octal numbers.

POSIXly:

case $1 in
  ("" | *[!0123456789]*)
    echo >&2 Sorry, decimal integer only
    exit 1
esac

Do not use [0-9] which often matches a lot more than [0123456789] (especially with bash globs, YMMV for bash's [[ =~ ]] operator which uses the system regexps where [0-9] may or may not match more than 0123456789 depending on the system and locale).

Note that you may also want to reject numbers like 08 or 09 which some tools (including bash arithmetic operators) reject as invalid octal numbers.

POSIXly:

case $1 in
  ("" | *[!0123456789]*)
    echo >&2 Sorry, decimal integer only
    exit 1
esac

Do not use [0-9] which often matches a lot more than [0123456789] (especially with bash globs, YMMV for bash's [[ =~ ]] operator which uses the system regexps where [0-9] may or may not match more than 0123456789 depending on the system and locale).

More on that at what is the meaning of this shell script function

Note that you may also want to reject numbers like 08 or 09 which some tools (including bash arithmetic operators) reject as invalid octal numbers.

Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

POSIXly:

case $1 in
  ("" | *[!0123456789]*)
    echo >&2 Sorry, decimal integer only
    exit 1
esac

Do not use [0-9] which often matches a lot more than [0123456789] (especially with bash globs, YMMV for bash's [[ =~ ]] operator which uses the system regexps where [0-9] may or may not match more than 0123456789 depending on the system and locale).

Note that you may also want to reject numbers like 08 or 09 which some tools (including bash arithmetic operators) reject as invalid octal numbers.