Why does export -p exclude $_ variable?
$_
does not seem to be an environmental variable in bash
, bash
only appears to export it into a child process' environment. Inside bash itself it seems to be a normal shell variable. Note however this is not the case when the first command is executed:
$ bash -c 'export -p | grep _='
declare -x _="/bin/bash"
Afterwards however it shows up as a normal variable:
$ bash -c ':; declare -p | grep _='
declare -- BASH_EXECUTION_STRING=":; declare -p | grep _="
declare -- _=":
Not this is not the case in dash
:
$ dash -c 'export -p | grep _='
export _='/bin/dash'
$ dash -c ':; export -p | grep _='
export _='/bin/dash'
Although here it only seems to take on its proper role in interactive mode:
$ dash
$ :
$ export -p | grep _=
export _=':'
export -p
does not show $_
for the simple reason that the command only shows those variables marked for export, and $_
(being a special parameter and not a variable--yes, the bash
documentation makes that distinction) is not marked for export by the shell. While you can assign to _
, bash
will overwrite its value after each command. bash
also seems to prevent, or at least undo, any attempt to explicitly export _
.