In bash, why can't _ be exported to the environment of the shell?
$ export _
$ export | grep _=
$
output nothing. Or do I miss something?
How can I export _ into the environment?
See also here. Thanks.
$_ is a special parameter, like $1, $- etc. It happens to be implemented as a variable in Bash, but it shouldn’t be thought of as such. See the special parameters section in the Bash manual:
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
Special parameters are not variables.
You can’t export it because Bash enforces that: every time a command is parsed, the exported flag is cleared on the _ variable.
Because $_ is a special parameter of the Bash shell, not a generic variable.
Related question: When is `_` an environment variable of a bash shell?