Understand the meaning of `$_`What does $_, and $0 represent in sourced bash?When is `_` dropped out of the...

Compaq Portable vs IBM 5155 Portable PC

How to politely tell someone they did not hit "reply to all" in an email?

What does ${!#} mean in Shell scripting?

Pirate democracy at its finest

Is the field of q-series 'dead'?

Is it rude to call a professor by their last name with no prefix in a non-academic setting?

Apt - strange requests to d16r8ew072anqo.cloudfront.net:80

Which European Languages are not Indo-European?

Where have Brexit voters gone?

Does pair production happen even when the photon is around a neutron?

Remove CiviCRM and Drupal links / banner on profile form

The art of clickbait captions

NIntegrate doesn't evaluate

Is it true that cut time means "play twice as fast as written"?

Is there an online tool which supports shared writing?

Can I connect my older mathematica front-end to the free wolfram engine?

How to cut a climbing rope?

What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?

Why do Russians almost not use verbs of possession akin to "have"?

Could a 19.25mm revolver actually exist?

How to reverse input order?

Defining the standard model of PA so that a space alien could understand

Why did the person in charge of a principality not just declare themself king?

Is it truly impossible to tell what a CPU is doing?



Understand the meaning of `$_`


What does $_, and $0 represent in sourced bash?When is `_` dropped out of the environment?When is `_` an environment variable of a bash shell?Usage of `_` as an environment variable passed to a commandWhat can we use “mail” for?What does `echo $_` output after `export foo=bar`?If there is no assignment of environment variable following `env`, what kind of environment is a command executed with?Why is variable assignment preceding command ignored?Can 'ssh <host> <command>' be configured to always load server-side startup files?$_ vs !$. Last argument of the preceding command and output redirectionWhat is the meaning of $0 in the Bash shellWhy does `bash -c 'echo $0 ' ` output “bash”?What does `echo $_` output after `export foo=bar`?What is the execution environment where `command [arguments]` in `exec command [arguments]` runs?Why is this environment variable not set up?Are all the shell options not inherited by scripts?When is `_` an environment variable of a bash shell?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







8















Could you explain the following sentences from Bash manual about $_, especially the parts in bold, maybe with some examples?





  1. At shell startup, set to the absolute pathname used to
    invoke the shell or shell script being executed as passed in the environment
    or argument list
    .




  2. Subsequently, expands to the last argument to the previous
    command, after expansion.




  3. Also set to the full pathname used to invoke each
    command executed
    and placed in the environment exported to that command.




  4. When checking mail, this parameter holds the name of the mail file.












share|improve this question




















  • 3





    -1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

    – Jeff Schaller
    May 2 '16 at 2:41


















8















Could you explain the following sentences from Bash manual about $_, especially the parts in bold, maybe with some examples?





  1. At shell startup, set to the absolute pathname used to
    invoke the shell or shell script being executed as passed in the environment
    or argument list
    .




  2. Subsequently, expands to the last argument to the previous
    command, after expansion.




  3. Also set to the full pathname used to invoke each
    command executed
    and placed in the environment exported to that command.




  4. When checking mail, this parameter holds the name of the mail file.












share|improve this question




















  • 3





    -1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

    – Jeff Schaller
    May 2 '16 at 2:41














8












8








8


3






Could you explain the following sentences from Bash manual about $_, especially the parts in bold, maybe with some examples?





  1. At shell startup, set to the absolute pathname used to
    invoke the shell or shell script being executed as passed in the environment
    or argument list
    .




  2. Subsequently, expands to the last argument to the previous
    command, after expansion.




  3. Also set to the full pathname used to invoke each
    command executed
    and placed in the environment exported to that command.




  4. When checking mail, this parameter holds the name of the mail file.












share|improve this question
















Could you explain the following sentences from Bash manual about $_, especially the parts in bold, maybe with some examples?





  1. At shell startup, set to the absolute pathname used to
    invoke the shell or shell script being executed as passed in the environment
    or argument list
    .




  2. Subsequently, expands to the last argument to the previous
    command, after expansion.




  3. Also set to the full pathname used to invoke each
    command executed
    and placed in the environment exported to that command.




  4. When checking mail, this parameter holds the name of the mail file.









bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 1 '16 at 21:03









jasonwryan

51.5k14136190




51.5k14136190










asked May 1 '16 at 20:52









TimTim

29.7k82274507




29.7k82274507








  • 3





    -1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

    – Jeff Schaller
    May 2 '16 at 2:41














  • 3





    -1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

    – Jeff Schaller
    May 2 '16 at 2:41








3




3





-1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

– Jeff Schaller
May 2 '16 at 2:41





-1 question unclear & not thoroughly researched. What have you tried? Where did your expectations misalign with the text? Do you not honestly understand the word 'subsequently'? If a new user were to ask this same question I would have the same questions of them (despite my similar innate curiosity as to exactly how things work).

– Jeff Schaller
May 2 '16 at 2:41










3 Answers
3






active

oldest

votes


















16














I agree it's not very clear.



1. At shell startup,





  • if the _ variable was in the environment that bash received, then bash leaves it untouched.



    In particular, if that bash shell was invoked by another bash
    shell (though zsh, yash and some ksh implementations also do
    it), then that bash shell will have set the _ environment
    variable to the path of the command being executed (that's the 3rd
    point in your question). For instance, if bash is invoked to
    interpret a script as a result of another bash shell interpreting:



    bash-script some args


    That bash will have passed _=/path/to/bash-scrip in the
    environment given to bash-script, and that's what the initial
    value of the $_ bash variable will be in the bash shell that
    interprets that script.



    $ env -i _=whatever bash -c 'echo "$_"'
    whatever



  • Now, if the invoking application doesn't pass a _ environment
    variable
    , the invoked bash shell will initialise $_ to the argv[0] it receives
    itself which could be bash, or /path/to/bash or
    /path/to/some-script or anything else (in the example above, that
    would be /bin/bash if the she-bang of the script was #! /bin/bash
    or /path/to/bash-script depending on the
    system).



    So that text is misleading as it describes the behaviour of the
    caller which bash has no control over. The application that invoked
    bash may very well not set $_ at all (in practice, only some
    shells and a few rare interactive applications do, execlp() doesn't
    for instance), or it could use it for something completely different
    (for instance ksh93 sets it to *pid*/path/to/command).



    $ env bash -c 'echo "$_"'
    /usr/bin/env (env did not set it to /bin/bash, so the value we
    get is the one passed to env by my interactive shell)
    $ ksh93 -c 'bash -c "echo $_"'
    *20042*/bin/bash



2. Subsequently



The Subsequently is not very clear either. In practice, that's as soon as bash interprets a simple command in the current shell environment.





  • In the case of an interactive shell, that will be on the first simple command interpreted from /etc/bash.bashrc for instance.



    For instance, at the prompt of an interactive shell:



     $ echo "$_"
    ] (the last arg of the last command from my ~/.bashrc)
    $ f() { echo test; }
    $ echo "$_"
    ] (the command-line before had no simple command, so we get
    the last argument of that previous echo commandline)
    $ (: test)
    $ echo "$_"
    ] (simple command, but in a sub-shell environment)
    $ : test
    $ echo "$_"
    test


  • For a non-interactive shell, it would be the first command in $BASH_ENV or of the code fed to that shell if $BASH_ENV is not
    set.



3. When Bash executes a command



The third point is something different and is hinted in the discussion above.



bash, like a few other shells will pass a _ environment variable to commands it executes that contains the path that bash used as the first argument to the execve() system calls.



$ env | grep '^_'
_=/usr/bin/env


4. When checking mail



The fourth point is described in more details in the description of the MAILPATH variable:




'MAILPATH'



A colon-separated list of filenames which the shell periodically
checks for new mail
.



Each list entry can specify the message that
is printed when new mail arrives in the mail file
by separating the
filename from the message with a '?'.
When used in the text of the
message, '$_' expands to the name of the current mail file.




Example:



$ MAILCHECK=1 MAILPATH='/tmp/a?New mail in <$_>' bash
bash$ echo test >> /tmp/a
New mail in </tmp/a>





share|improve this answer

































    4














    Try this simple example:



    echo "$_"
    echo "test"
    echo "$_"


    Run it by giving the absolute path (/home/$USERNAME/test.sh); output:



    /home/$USERNAME/test.sh
    test
    test


    First $_ contains the path you used to invoke the script and the second one contains the first argument to the middle echo.
    For the third item in the list, if you start a new terminal and type echo $_
    it will show the variable containing the PATH which is usually used to lookup and invoke commands (in my Ubuntu machine) in a normal shell or absolute path to your .bashrs file in a login shell.



    For the item four from bash man page:




    When used in the text of the message, $_ expands to the name of the
    current mailfile.







    share|improve this answer


























    • Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

      – Tim
      May 2 '16 at 0:00













    • Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

      – coffeMug
      May 2 '16 at 6:50











    • For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

      – coffeMug
      May 2 '16 at 7:18






    • 1





      FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

      – Jeff Schaller
      May 2 '16 at 9:47



















    0














    For case 2, an example is worth a thousand words:



    mkdir my_long_dir
    cd $_


    Does what you'd guess / hope for. Gives me a simple placeholder when I'm on the command line to save me having to repeate myself.






    share|improve this answer
























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f280453%2funderstand-the-meaning-of%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      16














      I agree it's not very clear.



      1. At shell startup,





      • if the _ variable was in the environment that bash received, then bash leaves it untouched.



        In particular, if that bash shell was invoked by another bash
        shell (though zsh, yash and some ksh implementations also do
        it), then that bash shell will have set the _ environment
        variable to the path of the command being executed (that's the 3rd
        point in your question). For instance, if bash is invoked to
        interpret a script as a result of another bash shell interpreting:



        bash-script some args


        That bash will have passed _=/path/to/bash-scrip in the
        environment given to bash-script, and that's what the initial
        value of the $_ bash variable will be in the bash shell that
        interprets that script.



        $ env -i _=whatever bash -c 'echo "$_"'
        whatever



      • Now, if the invoking application doesn't pass a _ environment
        variable
        , the invoked bash shell will initialise $_ to the argv[0] it receives
        itself which could be bash, or /path/to/bash or
        /path/to/some-script or anything else (in the example above, that
        would be /bin/bash if the she-bang of the script was #! /bin/bash
        or /path/to/bash-script depending on the
        system).



        So that text is misleading as it describes the behaviour of the
        caller which bash has no control over. The application that invoked
        bash may very well not set $_ at all (in practice, only some
        shells and a few rare interactive applications do, execlp() doesn't
        for instance), or it could use it for something completely different
        (for instance ksh93 sets it to *pid*/path/to/command).



        $ env bash -c 'echo "$_"'
        /usr/bin/env (env did not set it to /bin/bash, so the value we
        get is the one passed to env by my interactive shell)
        $ ksh93 -c 'bash -c "echo $_"'
        *20042*/bin/bash



      2. Subsequently



      The Subsequently is not very clear either. In practice, that's as soon as bash interprets a simple command in the current shell environment.





      • In the case of an interactive shell, that will be on the first simple command interpreted from /etc/bash.bashrc for instance.



        For instance, at the prompt of an interactive shell:



         $ echo "$_"
        ] (the last arg of the last command from my ~/.bashrc)
        $ f() { echo test; }
        $ echo "$_"
        ] (the command-line before had no simple command, so we get
        the last argument of that previous echo commandline)
        $ (: test)
        $ echo "$_"
        ] (simple command, but in a sub-shell environment)
        $ : test
        $ echo "$_"
        test


      • For a non-interactive shell, it would be the first command in $BASH_ENV or of the code fed to that shell if $BASH_ENV is not
        set.



      3. When Bash executes a command



      The third point is something different and is hinted in the discussion above.



      bash, like a few other shells will pass a _ environment variable to commands it executes that contains the path that bash used as the first argument to the execve() system calls.



      $ env | grep '^_'
      _=/usr/bin/env


      4. When checking mail



      The fourth point is described in more details in the description of the MAILPATH variable:




      'MAILPATH'



      A colon-separated list of filenames which the shell periodically
      checks for new mail
      .



      Each list entry can specify the message that
      is printed when new mail arrives in the mail file
      by separating the
      filename from the message with a '?'.
      When used in the text of the
      message, '$_' expands to the name of the current mail file.




      Example:



      $ MAILCHECK=1 MAILPATH='/tmp/a?New mail in <$_>' bash
      bash$ echo test >> /tmp/a
      New mail in </tmp/a>





      share|improve this answer






























        16














        I agree it's not very clear.



        1. At shell startup,





        • if the _ variable was in the environment that bash received, then bash leaves it untouched.



          In particular, if that bash shell was invoked by another bash
          shell (though zsh, yash and some ksh implementations also do
          it), then that bash shell will have set the _ environment
          variable to the path of the command being executed (that's the 3rd
          point in your question). For instance, if bash is invoked to
          interpret a script as a result of another bash shell interpreting:



          bash-script some args


          That bash will have passed _=/path/to/bash-scrip in the
          environment given to bash-script, and that's what the initial
          value of the $_ bash variable will be in the bash shell that
          interprets that script.



          $ env -i _=whatever bash -c 'echo "$_"'
          whatever



        • Now, if the invoking application doesn't pass a _ environment
          variable
          , the invoked bash shell will initialise $_ to the argv[0] it receives
          itself which could be bash, or /path/to/bash or
          /path/to/some-script or anything else (in the example above, that
          would be /bin/bash if the she-bang of the script was #! /bin/bash
          or /path/to/bash-script depending on the
          system).



          So that text is misleading as it describes the behaviour of the
          caller which bash has no control over. The application that invoked
          bash may very well not set $_ at all (in practice, only some
          shells and a few rare interactive applications do, execlp() doesn't
          for instance), or it could use it for something completely different
          (for instance ksh93 sets it to *pid*/path/to/command).



          $ env bash -c 'echo "$_"'
          /usr/bin/env (env did not set it to /bin/bash, so the value we
          get is the one passed to env by my interactive shell)
          $ ksh93 -c 'bash -c "echo $_"'
          *20042*/bin/bash



        2. Subsequently



        The Subsequently is not very clear either. In practice, that's as soon as bash interprets a simple command in the current shell environment.





        • In the case of an interactive shell, that will be on the first simple command interpreted from /etc/bash.bashrc for instance.



          For instance, at the prompt of an interactive shell:



           $ echo "$_"
          ] (the last arg of the last command from my ~/.bashrc)
          $ f() { echo test; }
          $ echo "$_"
          ] (the command-line before had no simple command, so we get
          the last argument of that previous echo commandline)
          $ (: test)
          $ echo "$_"
          ] (simple command, but in a sub-shell environment)
          $ : test
          $ echo "$_"
          test


        • For a non-interactive shell, it would be the first command in $BASH_ENV or of the code fed to that shell if $BASH_ENV is not
          set.



        3. When Bash executes a command



        The third point is something different and is hinted in the discussion above.



        bash, like a few other shells will pass a _ environment variable to commands it executes that contains the path that bash used as the first argument to the execve() system calls.



        $ env | grep '^_'
        _=/usr/bin/env


        4. When checking mail



        The fourth point is described in more details in the description of the MAILPATH variable:




        'MAILPATH'



        A colon-separated list of filenames which the shell periodically
        checks for new mail
        .



        Each list entry can specify the message that
        is printed when new mail arrives in the mail file
        by separating the
        filename from the message with a '?'.
        When used in the text of the
        message, '$_' expands to the name of the current mail file.




        Example:



        $ MAILCHECK=1 MAILPATH='/tmp/a?New mail in <$_>' bash
        bash$ echo test >> /tmp/a
        New mail in </tmp/a>





        share|improve this answer




























          16












          16








          16







          I agree it's not very clear.



          1. At shell startup,





          • if the _ variable was in the environment that bash received, then bash leaves it untouched.



            In particular, if that bash shell was invoked by another bash
            shell (though zsh, yash and some ksh implementations also do
            it), then that bash shell will have set the _ environment
            variable to the path of the command being executed (that's the 3rd
            point in your question). For instance, if bash is invoked to
            interpret a script as a result of another bash shell interpreting:



            bash-script some args


            That bash will have passed _=/path/to/bash-scrip in the
            environment given to bash-script, and that's what the initial
            value of the $_ bash variable will be in the bash shell that
            interprets that script.



            $ env -i _=whatever bash -c 'echo "$_"'
            whatever



          • Now, if the invoking application doesn't pass a _ environment
            variable
            , the invoked bash shell will initialise $_ to the argv[0] it receives
            itself which could be bash, or /path/to/bash or
            /path/to/some-script or anything else (in the example above, that
            would be /bin/bash if the she-bang of the script was #! /bin/bash
            or /path/to/bash-script depending on the
            system).



            So that text is misleading as it describes the behaviour of the
            caller which bash has no control over. The application that invoked
            bash may very well not set $_ at all (in practice, only some
            shells and a few rare interactive applications do, execlp() doesn't
            for instance), or it could use it for something completely different
            (for instance ksh93 sets it to *pid*/path/to/command).



            $ env bash -c 'echo "$_"'
            /usr/bin/env (env did not set it to /bin/bash, so the value we
            get is the one passed to env by my interactive shell)
            $ ksh93 -c 'bash -c "echo $_"'
            *20042*/bin/bash



          2. Subsequently



          The Subsequently is not very clear either. In practice, that's as soon as bash interprets a simple command in the current shell environment.





          • In the case of an interactive shell, that will be on the first simple command interpreted from /etc/bash.bashrc for instance.



            For instance, at the prompt of an interactive shell:



             $ echo "$_"
            ] (the last arg of the last command from my ~/.bashrc)
            $ f() { echo test; }
            $ echo "$_"
            ] (the command-line before had no simple command, so we get
            the last argument of that previous echo commandline)
            $ (: test)
            $ echo "$_"
            ] (simple command, but in a sub-shell environment)
            $ : test
            $ echo "$_"
            test


          • For a non-interactive shell, it would be the first command in $BASH_ENV or of the code fed to that shell if $BASH_ENV is not
            set.



          3. When Bash executes a command



          The third point is something different and is hinted in the discussion above.



          bash, like a few other shells will pass a _ environment variable to commands it executes that contains the path that bash used as the first argument to the execve() system calls.



          $ env | grep '^_'
          _=/usr/bin/env


          4. When checking mail



          The fourth point is described in more details in the description of the MAILPATH variable:




          'MAILPATH'



          A colon-separated list of filenames which the shell periodically
          checks for new mail
          .



          Each list entry can specify the message that
          is printed when new mail arrives in the mail file
          by separating the
          filename from the message with a '?'.
          When used in the text of the
          message, '$_' expands to the name of the current mail file.




          Example:



          $ MAILCHECK=1 MAILPATH='/tmp/a?New mail in <$_>' bash
          bash$ echo test >> /tmp/a
          New mail in </tmp/a>





          share|improve this answer















          I agree it's not very clear.



          1. At shell startup,





          • if the _ variable was in the environment that bash received, then bash leaves it untouched.



            In particular, if that bash shell was invoked by another bash
            shell (though zsh, yash and some ksh implementations also do
            it), then that bash shell will have set the _ environment
            variable to the path of the command being executed (that's the 3rd
            point in your question). For instance, if bash is invoked to
            interpret a script as a result of another bash shell interpreting:



            bash-script some args


            That bash will have passed _=/path/to/bash-scrip in the
            environment given to bash-script, and that's what the initial
            value of the $_ bash variable will be in the bash shell that
            interprets that script.



            $ env -i _=whatever bash -c 'echo "$_"'
            whatever



          • Now, if the invoking application doesn't pass a _ environment
            variable
            , the invoked bash shell will initialise $_ to the argv[0] it receives
            itself which could be bash, or /path/to/bash or
            /path/to/some-script or anything else (in the example above, that
            would be /bin/bash if the she-bang of the script was #! /bin/bash
            or /path/to/bash-script depending on the
            system).



            So that text is misleading as it describes the behaviour of the
            caller which bash has no control over. The application that invoked
            bash may very well not set $_ at all (in practice, only some
            shells and a few rare interactive applications do, execlp() doesn't
            for instance), or it could use it for something completely different
            (for instance ksh93 sets it to *pid*/path/to/command).



            $ env bash -c 'echo "$_"'
            /usr/bin/env (env did not set it to /bin/bash, so the value we
            get is the one passed to env by my interactive shell)
            $ ksh93 -c 'bash -c "echo $_"'
            *20042*/bin/bash



          2. Subsequently



          The Subsequently is not very clear either. In practice, that's as soon as bash interprets a simple command in the current shell environment.





          • In the case of an interactive shell, that will be on the first simple command interpreted from /etc/bash.bashrc for instance.



            For instance, at the prompt of an interactive shell:



             $ echo "$_"
            ] (the last arg of the last command from my ~/.bashrc)
            $ f() { echo test; }
            $ echo "$_"
            ] (the command-line before had no simple command, so we get
            the last argument of that previous echo commandline)
            $ (: test)
            $ echo "$_"
            ] (simple command, but in a sub-shell environment)
            $ : test
            $ echo "$_"
            test


          • For a non-interactive shell, it would be the first command in $BASH_ENV or of the code fed to that shell if $BASH_ENV is not
            set.



          3. When Bash executes a command



          The third point is something different and is hinted in the discussion above.



          bash, like a few other shells will pass a _ environment variable to commands it executes that contains the path that bash used as the first argument to the execve() system calls.



          $ env | grep '^_'
          _=/usr/bin/env


          4. When checking mail



          The fourth point is described in more details in the description of the MAILPATH variable:




          'MAILPATH'



          A colon-separated list of filenames which the shell periodically
          checks for new mail
          .



          Each list entry can specify the message that
          is printed when new mail arrives in the mail file
          by separating the
          filename from the message with a '?'.
          When used in the text of the
          message, '$_' expands to the name of the current mail file.




          Example:



          $ MAILCHECK=1 MAILPATH='/tmp/a?New mail in <$_>' bash
          bash$ echo test >> /tmp/a
          New mail in </tmp/a>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 8 '17 at 8:25

























          answered May 2 '16 at 11:24









          Stéphane ChazelasStéphane Chazelas

          319k57604972




          319k57604972

























              4














              Try this simple example:



              echo "$_"
              echo "test"
              echo "$_"


              Run it by giving the absolute path (/home/$USERNAME/test.sh); output:



              /home/$USERNAME/test.sh
              test
              test


              First $_ contains the path you used to invoke the script and the second one contains the first argument to the middle echo.
              For the third item in the list, if you start a new terminal and type echo $_
              it will show the variable containing the PATH which is usually used to lookup and invoke commands (in my Ubuntu machine) in a normal shell or absolute path to your .bashrs file in a login shell.



              For the item four from bash man page:




              When used in the text of the message, $_ expands to the name of the
              current mailfile.







              share|improve this answer


























              • Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

                – Tim
                May 2 '16 at 0:00













              • Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

                – coffeMug
                May 2 '16 at 6:50











              • For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

                – coffeMug
                May 2 '16 at 7:18






              • 1





                FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

                – Jeff Schaller
                May 2 '16 at 9:47
















              4














              Try this simple example:



              echo "$_"
              echo "test"
              echo "$_"


              Run it by giving the absolute path (/home/$USERNAME/test.sh); output:



              /home/$USERNAME/test.sh
              test
              test


              First $_ contains the path you used to invoke the script and the second one contains the first argument to the middle echo.
              For the third item in the list, if you start a new terminal and type echo $_
              it will show the variable containing the PATH which is usually used to lookup and invoke commands (in my Ubuntu machine) in a normal shell or absolute path to your .bashrs file in a login shell.



              For the item four from bash man page:




              When used in the text of the message, $_ expands to the name of the
              current mailfile.







              share|improve this answer


























              • Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

                – Tim
                May 2 '16 at 0:00













              • Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

                – coffeMug
                May 2 '16 at 6:50











              • For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

                – coffeMug
                May 2 '16 at 7:18






              • 1





                FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

                – Jeff Schaller
                May 2 '16 at 9:47














              4












              4








              4







              Try this simple example:



              echo "$_"
              echo "test"
              echo "$_"


              Run it by giving the absolute path (/home/$USERNAME/test.sh); output:



              /home/$USERNAME/test.sh
              test
              test


              First $_ contains the path you used to invoke the script and the second one contains the first argument to the middle echo.
              For the third item in the list, if you start a new terminal and type echo $_
              it will show the variable containing the PATH which is usually used to lookup and invoke commands (in my Ubuntu machine) in a normal shell or absolute path to your .bashrs file in a login shell.



              For the item four from bash man page:




              When used in the text of the message, $_ expands to the name of the
              current mailfile.







              share|improve this answer















              Try this simple example:



              echo "$_"
              echo "test"
              echo "$_"


              Run it by giving the absolute path (/home/$USERNAME/test.sh); output:



              /home/$USERNAME/test.sh
              test
              test


              First $_ contains the path you used to invoke the script and the second one contains the first argument to the middle echo.
              For the third item in the list, if you start a new terminal and type echo $_
              it will show the variable containing the PATH which is usually used to lookup and invoke commands (in my Ubuntu machine) in a normal shell or absolute path to your .bashrs file in a login shell.



              For the item four from bash man page:




              When used in the text of the message, $_ expands to the name of the
              current mailfile.








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 1 '16 at 22:31

























              answered May 1 '16 at 20:57









              coffeMugcoffeMug

              7,583112951




              7,583112951













              • Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

                – Tim
                May 2 '16 at 0:00













              • Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

                – coffeMug
                May 2 '16 at 6:50











              • For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

                – coffeMug
                May 2 '16 at 7:18






              • 1





                FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

                – Jeff Schaller
                May 2 '16 at 9:47



















              • Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

                – Tim
                May 2 '16 at 0:00













              • Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

                – coffeMug
                May 2 '16 at 6:50











              • For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

                – coffeMug
                May 2 '16 at 7:18






              • 1





                FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

                – Jeff Schaller
                May 2 '16 at 9:47

















              Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

              – Tim
              May 2 '16 at 0:00







              Thanks. (1) can you explain the third case with examples? (2) When I open a new gnome terminal tab, the output of echo $_ is EDITOR, and why is it? Which case does it follow? (3) Can you given an example of how to check mails, and of using $_ in this case?

              – Tim
              May 2 '16 at 0:00















              Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

              – coffeMug
              May 2 '16 at 6:50





              Open your .bashrc and comment out every line there. Keep only a single line containing a command like echo "test" or similar. Save and close. Then run bash and immediately echo $_. This last echo should print test in the output.

              – coffeMug
              May 2 '16 at 6:50













              For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

              – coffeMug
              May 2 '16 at 7:18





              For the mail part I have not used Unix mail and don't have a working mail installed on my system now so cannot give you example. But should be obvious if you follow the description of MAILPATH in linuxcommand.org/lc3_man_pages/bash1.html

              – coffeMug
              May 2 '16 at 7:18




              1




              1





              FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

              – Jeff Schaller
              May 2 '16 at 9:47





              FYI, the canonical, up-to-date version of the bash manual is: gnu.org/software/bash/manual/bashref.html . It's version 4.3 while the linuxcommand page was 4.1.

              – Jeff Schaller
              May 2 '16 at 9:47











              0














              For case 2, an example is worth a thousand words:



              mkdir my_long_dir
              cd $_


              Does what you'd guess / hope for. Gives me a simple placeholder when I'm on the command line to save me having to repeate myself.






              share|improve this answer




























                0














                For case 2, an example is worth a thousand words:



                mkdir my_long_dir
                cd $_


                Does what you'd guess / hope for. Gives me a simple placeholder when I'm on the command line to save me having to repeate myself.






                share|improve this answer


























                  0












                  0








                  0







                  For case 2, an example is worth a thousand words:



                  mkdir my_long_dir
                  cd $_


                  Does what you'd guess / hope for. Gives me a simple placeholder when I'm on the command line to save me having to repeate myself.






                  share|improve this answer













                  For case 2, an example is worth a thousand words:



                  mkdir my_long_dir
                  cd $_


                  Does what you'd guess / hope for. Gives me a simple placeholder when I'm on the command line to save me having to repeate myself.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 14 mins ago









                  dszdsz

                  1693




                  1693






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f280453%2funderstand-the-meaning-of%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...