[Lvlug] bash question: eval command
Paul Ryan
paul.ryan at usa.net
Thu Sep 29 21:47:31 EDT 2005
On the first pass the $ must be escaped so they are not replaced. On eval's
second go round, the command passed for execution will look like:
sort -nr $1 | head -$2
and the correct substitution will take place.
Paul
------ Original Message ------
Received: Thu, 29 Sep 2005 07:13:27 PM EDT
From: Brian Reichert <rignes at ptd.net>
To: LVLUG <lvlug at thelinuxlink.net>
Subject: [Lvlug] bash question: eval command
I'm going through an example in Learning the bash Shell that talks about the
eval command and the order of processing of a command line. First, here is
the
command example:
$ eval sort -nr \$1 ${2:+"| head -\$2"}
Basically, what it's getting at is that variable substitution doesn't happen
until after the part where the command is broken into tokens (where | is
recognized). Thus, what would normally happen without eval is the | would be
considered an argument instead of doing it's normal job of directing stdout
of
sort into stdin of head. What eval does it makes the command line processing
start over so it will use the | correctly. So, if $2 is present it turns the
command into:
$ eval sort -nr \$1 | head -\$2
What is confusing me is that the $ are escaped. Why does it work? What is
different about eval that it still does the variable substitution even with
the
$ part of the variable escaped? I've read over the section of the book about
6
times so far and still don't get that part.
Can someone give me an easy to get explanation? Or maybe a resource that
will
lead me to getting it?
Thanks.
--
Brian Reichert
http://rignesnet.tzo.com
If you want divine justice, die.
-- Nick Seldon
> ---------------------------------------------
> Attachment: application/pgp-signature
> MIME Type: application/pgp-signature
> ---------------------------------------------
More information about the Lvlug
mailing list