On Wed, Jul 23, 2008 at 12:57 PM, Michael ODonnell
<[EMAIL PROTECTED]> wrote:
>  "When bash invokes an external command, the variable $_ is set to
>  the full file name of the command and passed to that command in its
>  environment."

  That appears to function as described.  I wrote a small program to test:

/* underscore.c */
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char ** argv) {
printf("_=<%s>\n", getenv("_"));
}

  The result:

$ underscore
_=</home/bscott/bin/underscore>
$ ~/bin/underscore
_=</home/bscott/bin/underscore>
$ ./bin/underscore
_=<./bin/underscore>
$ underscore one
_=</home/bscott/bin/underscore>
$ underscore one two
_=</home/bscott/bin/underscore>
$

> ...which seems to describe one piece of how it actually works in one
> situation.  For example, if I create and execute a shell script thus:

  As VirginShow points out, there's a difference between bash
executing an external program, and what you do from a shell script or
prompt.  For one, "echo" is a bash built-in.  Demonstration:

$ echo --version
--version
$ enable -n echo
$ echo --version
echo (GNU coreutils) 5.97

  But it's more than that.  When type "$_" at the shell prompt or in a
script, you're asking bash to *evaluate* the underscore variable.  So
even if you're invoking an external command, the evaluation will be
done by bash.  As the manual doesn't document what happens for that, I
would guess the behavior is "undefined".

  As Thomas Charon hinted at, it appears to start out set to the last
external command, but then it gets set to the last word of the
previous command line.  I dunno if that's "by design", or just an
accident of implemenetation.

$ enable -n true
$ enable echo
$ true
$ echo one /$_/ three
one /true/ three
$ echo one /$_/ three
one /three/ three
$ echo one /$_/ three
one /three/ three
$

-- Ben
_______________________________________________
gnhlug-discuss mailing list
gnhlug-discuss@mail.gnhlug.org
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/

Reply via email to