I'm following through a tutorial and it mentions to run this command:
sudo chmod 700 !$
I'm not familiar with !$
. What does it mean?
bashcommand history
I'm following through a tutorial and it mentions to run this command:
sudo chmod 700 !$
I'm not familiar with !$
. What does it mean?
Best Answer
Basically, it's the last argument to the previous command.
(source; handy guide, by the way)
It's worth nothing the distinction between this
!$
token and the special shell variable$_
. Indeed, both expand to the last argument of the previous command. However,!$
is expanded during history expansion, while$_
is expanded during parameter expansion. One important consequence of this is that, when you use!$
, the expanded command is saved in your history.For example, consider the keystrokes
echo Foo
Enterecho !$ Jar
Enter Up Enter; andecho Foo
Enterecho $_ Jar
Enter Up Enter.(The only characters changed are the
$!
and$_
in the middle.)In the former, when you press Up, the command line reads
echo Foo Jar
, so the last line written to stdout isFoo Jar
.In the latter, when you press Up, the command line reads
echo $_ bar
, but now$_
has a different value than it did previously—indeed,$_
is nowJar
, so the last line written to stdout isJar Jar
.Another consequence is that
_
can be used in other parameter expansions, for example, the sequence of commandsprints
isomorphism isosceles
. But there's no analogous "${!$%morphism}
" expansion.For more information about the phases of expansion in Bash, see the
EXPANSION
section ofman 1 bash
(this is called Shell Expansions in the online edition). TheHISTORY EXPANSION
section is separate.