18 July 2021

In a previous post I described command bc to evaluate mathematical expression.

There is another command that could be used to check if variable is integer, mathematical (only integer) and generic expression:
expr

In general expr print the value of EXPRESSION to standard output.
Note: all example below are execute in a container:

docker run -it  ubuntu /bin/bash

Check if variable is integer

Case positive integer:

$ expr "2" + 0
2

Case NOT integer:

$ expr "foo" + 0
expr: non-integer argument

Case negative integer:

$ expr "-2" + 0
-2

Mathematical operations

Sum:

# expr 2 + 3
5

Division

# expr 10 / 3
3

Check regular expression

# expr 'ipsedixit.org' : '\(.*\)\.org'
ipsedixit

References