[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What should be the expected behavior for $_ ?
From: |
Chet Ramey |
Subject: |
Re: What should be the expected behavior for $_ ? |
Date: |
Tue, 3 Apr 2018 10:55:49 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 |
On 4/3/18 10:03 AM, Siteshwar Vashisht wrote:
> According to bash reference manual[1]:
>
> ($_, an underscore.) 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. Subsequently, expands to the last argument to the previous
> command, after expansion. Also set to the full pathname used to invoke each
> command executed and placed in the environment exported to that command. When
> checking mail, this parameter holds the name of the mail file.
>
> Now consider following example (with bash-completion package installed):
>
> $ cd /tmp/
> $ touch rpmall.txt rpmshort.txt
> $ mkdir testdir
> $ cp rpmall.txt rpmshort.txt $_ # Use tab completion to complete filenames
> cp: target '_filedir' is not a directory
>
> Last command fails because tab completing 'cp' command modifies value of
> '$_'. Shall value of '$_' be modified if a command gets executed in
> background ?
It's an interesting question. You want $_ to expand to the last argument
(or last word) of the previous history entry when the shell is interactive,
which is available as !$, instead of the last command executed by the
current shell instance.
Should the command line know about shell functions and commands executed in
the foreground on its behalf? What should the behavior be in a
non-interactive shell? What do folks think?
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/