[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: what's $_ in bash



On Sat, 8 Apr 2023 tom@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

Reply to: