Bash – the last argument of the previous command

bash

$_ is said to be the last argument of the previous command.

So I wonder why it is not EDITOR="emacs -nw" but EDITOR in the following example?

Why isn't "emacs -nw" part of the last argument?

More generally, what are the definitions of an argument, and the last argument?

Thanks.

$ export EDITOR="emacs -nw"
$ echo $_
EDITOR

Best Answer

Bash processes variable assignments, when they’re allowed as arguments (with alias, declare, export, local, readonly, and typeset), before anything else (or rather, it identifies them before anything else — expansion applies to the values assigned to variables). When it gets to word expansion, the remaining command is export EDITOR, so _ is set to EDITOR.

Generally speaking, arguments are the “words” remaining after expansion (which doesn’t include variable assignments and redirections).

See Simple command expansion in the Bash manual for details.