path: root/bin/expr
diff options
authorJilles Tjoelker <jilles@FreeBSD.org>2010-09-09 21:59:53 +0000
committerJilles Tjoelker <jilles@FreeBSD.org>2010-09-09 21:59:53 +0000
commitf390d8d927e2bc6a8afd086bfdcdbf5edcbc6875 (patch)
tree5f14448517b12ac1355971cc43dc4f73520e6aa8 /bin/expr
parentf0a7fc8b30b44bf683647aca6daa9dc75bd0523b (diff)
expr(1): Add sh(1) versions of examples, remove an incorrect example.
The three examples are better done using sh(1) itself these days. The example expr -- "$a" : ".*" is incorrect in the general case, as "$a" may be an operator. MFC after: 2 weeks
Notes: svn path=/head/; revision=212390
Diffstat (limited to 'bin/expr')
1 files changed, 21 insertions, 10 deletions
diff --git a/bin/expr/expr.1 b/bin/expr/expr.1
index 3f8eabbdde66..5c3cb4471e40 100644
--- a/bin/expr/expr.1
+++ b/bin/expr/expr.1
@@ -30,7 +30,7 @@
.\" $FreeBSD$
-.Dd July 12, 2004
+.Dd September 9, 2010
.Dt EXPR 1
@@ -217,6 +217,9 @@ command, one might rearrange the expression:
More generally, parenthesize possibly-negative values:
.Dl "a=$(expr \e( $a \e) + 1)"
+With shell arithmetic, no escaping is required:
+.Dl "a=$((a + 1))"
This example prints the filename portion of a pathname stored
in variable
.Va a .
@@ -229,6 +232,12 @@ The
.Li //
characters resolve this ambiguity.
.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
+With modern
+.Xr sh 1
+.Dl "\*q${a##*/}\*q"
+expands to the same value.
The following examples output the number of characters in variable
@@ -237,19 +246,21 @@ Again, if
.Va a
might begin with a hyphen, it is necessary to prevent it from being
interpreted as an option to
-.Nm .
+.Nm ,
+.Va a
+might be interpreted as an operator.
.Bl -bullet
-If the
-command conforms to
-.St -p1003.1-2001 ,
-this is simple:
-.Dl "expr -- \*q$a\*q \&: \*q.*\*q"
-For portability to older systems, however, a more complicated command
+To deal with all of this, a complicated command
is required:
.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
+With modern
+.Xr sh 1
+syntax, this can be done much more easily:
+.Dl "${#a}"
+expands to the required number.
.Xr sh 1 ,