Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what's $_ in bash

142 views
Skip to first unread message

t...@myposts.ovh

unread,
Apr 7, 2023, 11:50:05 PM4/7/23
to
Hello

in bash shell, what's "$_" variable? where defines it?

Thanks.

davidson

unread,
Apr 8, 2023, 12:50:07 AM4/8/23
to
On Sat, 8 Apr 2023 t...@myposts.ovh wrote:
> Hello
>
> in bash shell, what's "$_" variable?

I'd be interested to learn as well.

> where defines it?

In the meantime you can read in

$ man bash

under section "PARAMETERS", subheading "Shell Variables":

Shell Variables
The following variables are set by the shell:

_ At shell startup, set to the pathname
used to invoke the shell or shell script
being executed as passed in the environ‐
ment or argument list. Subsequently,
expands to the last argument to the pre‐
vious simple command executed in the
foreground, after expansion.

This last bit above means that you can pass the last argument (as
determined post-expansion) of the previous command to the one you are
about to enter.

So, like, if you like to keep soft links to what you're reading in
directory "readinglist",

$ ln -Ts ~/web/downloads/UnicodeStandard-15.0.pdf \
readinglist/unicodestd15.pdf
$ mupdf "$_" # display the pdf

That is just a contrived example for illustrating the meaning. (Typing
"mupdf " followed by ALT-_ is more efficient.) I am as curious as you
are, how it gets used in practice.

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

This last bit means, I guess, that a command can tell "what its own
name is", or what the invoker thinks it is.

When checking mail, this parameter holds
the name of the mail file currently being
checked.

--
Hackers are free people. They are like artists. If they are in a good
mood, they get up in the morning and begin painting their pictures.
-- Vladimir Putin

Kent West

unread,
Apr 8, 2023, 8:00:06 AM4/8/23
to


On 4/7/23 23:40, davidson wrote:
On Sat, 8 Apr 2023 t...@myposts.ovh wrote:
Hello

in bash shell, what's "$_" variable?


kent@westk-9463:~$ ls *html
morsekeyer.html  morse.html  myGameEasier.html  myGame.html

The 'ls *html' "expands" to "ls morsekeyer.html morse.html myGameEasier.html myGame.html", which means that the last item to that last command was "myGame.html", as we see below from echoing "$_".

kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- myGame.html.



Below, "*kent" does not expand any further, so "*kent" gets returned by the "$_".

kent@westk-9463:~$ ls *kent
ls: cannot access '*kent': No such file or directory
kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- *kent.



And below is a final example:

kent@westk-9463:~$ ls *conf
syslog.conf

kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- syslog.conf.


Yassine Chaouche

unread,
May 2, 2023, 3:50:05 AM5/2/23
to
Le 4/8/23 à 04:35, t...@myposts.ovh a écrit :
> Hello
>
> in bash shell, what's "$_" variable? where defines it?
>
> Thanks.
>

It's the last argument of the last command.
I use it often when installing a package.
Something like:
$ apt-cache show "package-name"
[... read the description ...]
$ sudo apt-get install $_


By the way you can also use Alt-. to yank (paste) the last argument.
Keep pressing Alt-. to go through previous arguments.

Best,

--
yassine -- sysadm
+213-779 06 06 23
http://about.me/ychaouche
Looking for side gigs.
0 new messages