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?
$FOO
, then export the latter.
– dr_
Apr 12 '18 at 12:07
export
not work with any special parameter? Where in the official document does it say so?
– Tim
Apr 12 '18 at 12:08
_
) is not allowed? – Tim Apr 13 '18 at 05:06_
is a special parameter, and bash can export it when executing an external executable or script. Do you mean all special parameters except_
in that case can't be exported? – Tim Apr 13 '18 at 12:44_
is a special parameter, and special parameters aren’t exported. Bash separately places a variable named_
, storing the path of the command being run, into child process’ environments when they start. I know the manual mixes everything up in the description of the_
special parameter, but it’s easier to understand (and closer to reality) to think of the_
special parameter on the one hand, and the_
variable exported to commands on the other. – Stephen Kitt Apr 13 '18 at 12:50_
and variable_
can't exit at the same time, correct? – Tim Apr 13 '18 at 14:26