mkdir tmp && cd "$_"
is what you're looking for, I believe (you can drop the double quotes if you're sure that quoting is not needed).
$_
expands differently in different contexts, but the one that matters here (from man bash
, v3.2.51):
expands to the last argument to the previous command, after expansion.
Background:
Note: the following discusses bash
, but it seems to apply to zsh
in principle as well.
$_
is a so-called special parameter, which you'll find listed among others (without the $
prefix) in the Special Parameters
section of man bash
:
- Works both in interactive shells and in scripts.
- Works at the individual command level, not at the line level.
By contrast, !
-prefixed tokens (such as !$
to recall the most recent line's last token) relate to the shell's [command] history expansion (section HISTORY EXPANSION
of man bash
):
- By default they work only in interactive shells, but that can be changed with
set -o history
andset -o histexpand
(thanks, @chepner)). - Work at the line level (more accurately: everything that was submitted with one
Enter
keystroke, no matter how many individual commands or lines it comprised).