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

~ /home 🏠 ~ talks 💬 ~ bash ~ craftsmanship ~ db ~ dongxi ~ emacs ~ escenic ~ iam ~ java ~ js ~ language ~ latex ~ ldap ~ life ~ linux ~ llm ~ mac-os-x ~ mt-foo ~ network ~ norsk ~ python ~ quotes ~ running ~ security ~ travel ~ unix ~ various ~ vcs ~ webdesign ~ windows ~ discoveries ~ cv 🧙 ~

Licensed under CC BY Creative Commons License ~ 📡 RSS feed ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@hachyderm.io ~ 🎥 youtube.com/@skybert