Bash Manual says (manpage, my emphasis):
When Bash invokes an external command, the variable
$_
is set to the full pathname of the command and passed to that command in its environment.
And (Special Parameters):
_
(
$_
, 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.
In a bash shell, I run:
$ bash $ export | grep '_='
According to the manual,
_
should be an environment variable of the new bash shell.export
is supposed to output all the environment variables of the new bash shell, but it doesn't output_
. So I wonder whether_
is an environment variable of the new bash shell?Actually in any bash shell, the same thing happens
$ export | grep '_='
doesn't output anything. So I wonder if
_
is ever an environment variable of a bash shell?For comparison:
$ dash $ export | grep '_=' export _='/bin/dash'
My post is inspired by Mike's comment and Stephane's reply.
export
is a builtin, but if you useprintenv _
, it will show you how it was invoked:/usr/bin/printenv
on this system.bash -c export | grep _=
(from Bash), will show how the parent shell invoked thebash
command, even though$_
is unset in the parent.