Skip to content

Instantly share code, notes, and snippets.

@bernardolm
Last active July 6, 2022 05:31
Show Gist options
  • Save bernardolm/1c9e003a5f68e6e2534458fa758a096d to your computer and use it in GitHub Desktop.
Save bernardolm/1c9e003a5f68e6e2534458fa758a096d to your computer and use it in GitHub Desktop.
input=(73 foobar)
rg="^[+\-]{0,1}[0-9]+$"
for num in "${input[@]}"; do
(echo "${num//[^\-+0-9]/}" | grep -Eq $rg) \
&& echo "$num is a number" \
|| echo "$num is not a number";
nnum="-"$num
(echo "${nnum//[^\-+0-9]/}" | grep -Eq $rg) \
&& echo "$nnum is a number" \
|| echo "$nnum is not a number";
pnum="+"$num
(echo "${pnum//[^\-+0-9]/}" | grep -Eq $rg) \
&& echo "$pnum is a number" \
|| echo "$pnum is not a number";
npnum="-+"$num
(echo "${npnum//[^\-+0-9]/}" | grep -Eq $rg) \
&& echo "$npnum is a number" \
|| echo "$npnum is not a number";
done
@bernardolm
Copy link
Author

will output:

73 is a number
-73 is a number
+73 is a number
-+73 is not a number
foobar is not a number
-foobar is not a number
+foobar is not a number
-+foobar is not a number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment