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.
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.
_) is not allowed?
_ 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?
_ 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.
_ and variable _ can't exit at the same time, correct?
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.
export not work with any special parameter? Where in the official document does it say so?