Numbers in BASH


Testing if a variable is numeric

local days=$1
if [[ $days =~ ^[0-9]*$ ]]; then
   echo "You entered number of days: ${days}"
fi

Testing if a variable is not numeric

local days=$1
if [[ ! $days =~ ^[0-9]*$ ]]; then
  echo "Usage: $(basename $0) <number-of-days>"
  exit 1
fi

Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@emacs.ch ~ 🐦 @torsteinkrause