[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FW: expr broken

From: Bob Proulx
Subject: Re: FW: expr broken
Date: Fri, 4 Oct 2002 20:11:23 -0600
User-agent: Mutt/1.4i

Dee-Ann LeBlanc <address@hidden> [2002-10-02 18:24:56 -0700]:
> I forwarded this to some folks who are helping me with scripting and
> thought I'd forward it to the proper bug reporting agency too.

You found it!  Thanks for the report.

> Obviously not all of these are your problem, but the RH 8 version I
> was using gives this address, so this is a problem in your expr unless
> something else is going on.

Actually it is not a problem with expr.  You have run into a common
misconception about your shell.

> > Okay, I've discovered the oddest thing. Type the following
> > at any command line where the "expr" mathematical
> > expression evaluation routine is used:
> >
> > expr 4 + 3
> >
> > This will work.

Yes.  No special characters there.

> > But type:
> >
> > expr 4 * 3
> >
> > This gives me a syntax error in:

Yes.  The '*' is a file wildcard.  It will be expanded by the shell to
match files.  That is what makes commands like this work.

  cp * /tmp/somedir/  # Do _NOT_ try.  Just for example.

Try this:

  mkdir /tmp/myplayarea && cd /tmp/myplayarea
  touch apple orange pear
  echo expr 4 * 3
  expr 4 apple orange pear 3

The echo will show you what the expr command sees when it tries to run
that command.  As you can see it is no wonder that it reports a syntax

> > Can anyone else give me some data points? Other forms of
> > Linux and Unix? Other shells? I look at all of the man
> > pages and this is the correct format, and related items
> > (such as % for division remainder) work.

The shell interpreter is expanding the command line glob characters
prior to handing the arguments off to your command.  This is a simple
form of regular expression matching designed to make file name
matching easier.  This provides a consistent interface to all programs
since the expansion code is common to all programs by being in the
interpreting shell instead of in the program itself.

Commands in UNIX do not see the '*.html' or any of the shell
metacharacters.  Commands see the expanded out names which the shell
found matched file names in current directory.  The '*' is the "glob"
character because it matches a glob of characters.  The shell matches
and expands glob characters and hands of the resulting information to
the command.

You need to protect the '*' from shell expansion.

Try this:

  expr 4 '*' 3
  expr 4 "*" 3
  expr 4 \* 3

All three of those quoting styles will protect the wildcard from
expansion.  Pick the one you like the best.  I usually use the first
since it is an unshifted key on my keyboard and looks nicest to me.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]