[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'expr' command not working with option *
From: |
Bob Proulx |
Subject: |
Re: 'expr' command not working with option * |
Date: |
Fri, 12 Aug 2005 22:46:16 -0600 |
User-agent: |
Mutt/1.5.9i |
Eric Blake wrote:
> > when i type expr 2 * 3 it is giving syntax error.
>
> This is turning into a FAQ; although the FAQ page at
> http://www.gnu.org/software/coreutils/faq/coreutils-faq.html doesn't
> mention expr by name, it does mention problems with * as a shell
> metacharacter, which needs to be quoted.
Thanks for motivating me to add an entry for the expr * case. It
has been getting asked a number of times of late. I just added the
following case to the FAQ.
2005-08-13 Bob Proulx <address@hidden>
* coreutils-faq.texinfo, coreutils-faq.html:
Added 'expr 2 * 3' example explaining need to quote shell
metacharacters.
Index: coreutils-faq.texinfo
===================================================================
RCS file: /webcvs/coreutils/coreutils/faq/coreutils-faq.texinfo,v
retrieving revision 1.12
diff -u -r1.12 coreutils-faq.texinfo
--- coreutils-faq.texinfo 13 Aug 2005 01:33:24 -0000 1.12
+++ coreutils-faq.texinfo 13 Aug 2005 04:36:27 -0000
@@ -1233,6 +1234,60 @@
@example
@uref{http://www.ietf.org/rfc/ien/ien137.txt}
@end example
+
address@hidden
---------------------------------------------------------------------------
address@hidden expr 2 * 3 does not work
address@hidden expr 2 * 3 does not work
+
+The expr program appears to be broken because expr 2 * 3 produces a
+syntax error.
+
address@hidden
+ expr 2 * 3
+ expr: syntax error
address@hidden example
+
+The case shown is not quoted correctly. As such it provides a good
+example of incorrectly quoting shell metacharacters. The ``*'' is
+being expanded by the shell and the expr is seeing filenames creating
+the syntax error.
+
+The ``*'' character is a special character to the command shell. It
+is called a glob character because the shell expands it to match
+filenames. It matches a glob of characters. The shell then passes
+the result to the command. The expr program does not see the star
+character in ``2 * 3'' but instead sees a ``2'' followed by every
+filename matched from the current directory and then finally the
+``3''.
+
+Your command is really something completely different than you thought
+it was going to be. Use echo to see what your command line really is:
+
address@hidden
+ echo expr 2 * 3
address@hidden example
+
+You will see it matching filenames and so create the syntax error.
+
+There are many entries in this FAQ related to command line ``*'' glob
+expansion. If you are having trouble with this concept then read
+through the other entries for a fresh perspective.
+
+You need to quote shell metacharacters to prevent them from being
+modified by the shell. Here are some possible ways of quoting this
+example.
+
address@hidden
+ expr 2 \* 3
+ expr 2 '*' 3
+ expr 2 "*" 3
address@hidden example
+
+The man page for expr carries this text:
+
address@hidden
+Beware that many operators need to be escaped or quoted for shells.
address@hidden quotation
@c ---------------------------------------------------------------------------
@bye