command does a lot of different things with expressions.
One expression it handles has three arguments: first, a string; second,
a colon (
); third, a
regular expression (
The string and regular expression usually need quotes.
can count the number of characters that match the
The regular expression is automatically anchored to the
start of the string you're matching, as if you'd typed a
at the start of it in
, and so on.
is usually run with
to save its output:
part="resistor 321-1234-00" name="Ellen Smith"
expr "$part" : '[a-z ]*[0-9]'
...character position of first number
len=`expr "$name" : '[a-zA-Z]*'`
echo first name has $len characters
first name has 5 characters
matches some character(s),
a zero ("true")
exit status (
If you want a true/false test like this, throw away the number that
prints and test its exit status:
if expr "$part" : '.*[0-9]' > /dev/null
then echo \$part has a number in it.
else echo "it doesn't"
$part has a number in it.