Re: [man bash] help understanding.

From: Stephen Bach (sjbach_at_users.sourceforge.net)
Date: 04/27/05


Date: Wed, 27 Apr 2005 14:02:02 -0400

Hi nabis,

nabis wrote:
> Hi!
> I need some additional explanation on section PARAMETERS subsection
> Special Parameters from the bash(1) manual page. I states:
>
> _ At shell startup, set to the absolute file name of the shell or
> shell script being executed as passed in the argument list. Subse-
> quently, expands to the last argument to the previous command,
> after expansion. Also set to the full file name of each command
> executed and placed in the environment exported to that command.
> When checking mail, this parameter holds the name of the mail file
> currently being checked.
>
> If you translate it in code it will, probably, look like the following:
>
> #!/usr/bin/bash
> echo $_ # the absolute file name of the shell
> ls /home
> echo $_ # the last argument to the previous command
>
> I'd appreciate if someone could explain better or type in a line of code
> which demonstrates the following:
> "Also set to the full file name of each command executed and placed in
> the environment exported to that command."
>
> Thank you. Vitaliy.

It just means that the new process will see the $_ variable as being
the path to its executable (not as the last argument to the
command called previous to it).

For example, given a script and a short program:

foo.sh:
        #!/bin/bash
        echo $_
        ls /
        echo $_

        ./bar abc def ghi

        echo $_

bar.c:
        #include <unistd.h>
        #include <stdlib.h>
        #include <stdio.h>

        int main(int argc, char** argv) {

                printf("%s\n", getenv("_"));

                return 0;
        }

The output from running ./foo.sh:

        /bin/bash
        bin dev home lost+found opt root sys usr
        boot etc lib mnt proc sbin tmp var
        /
        ./bar
        ghi



Relevant Pages

  • Bash-4.0 available for FTP
    ... Unlike previous bash distributions, this tar file includes the formatted ... The shell has been changed to be more ... rigorous about parsing commands inside command substitutions, ... Changes have been made to the Readline library being released at ...
    (gnu.announce)
  • Why newbies dont RTFM...
    ... Even though I've used Linux before, I've never had to do any ... BASH BUILTIN COMMANDS ... last command exited within ... unless the shell is not exeâ ...
    (comp.os.linux.misc)
  • Re: Spawning process with environment variables
    ... starting the shell). ... command on the fly. ... a single-line script to a shell for execution. ... execution of shell scripts instead of writing shell script controlling ...
    (comp.unix.programmer)
  • Re: Great SWT Program
    ... None of the nasty things that you have said or implied about me are at ... treat the file as input (manually invoking the command interpreter ... script, copy the line into that within the editor, exit, and invoke ... the shell script. ...
    (comp.lang.java.programmer)
  • Re: ksh silently ignores function if mistakenly not autoloaded
    ... Here's the order of execution. ... This really takes place prior to command ... Shell scripts, with all their power, have one major drawback - they ... In the Korn Shell, there are two separate syntaxes for defining ...
    (comp.unix.shell)