0

Could somebody explain this output?

$ echo !
!
$ echo !!
echo echo !
echo !
$

I expected sedond echo to write only !!

3
  • If you view your command history & decide you want to run command 419 in that history, you can type !419 and it'll execute. Provided answers tell you !! executes your last command, but it has other uses too :)
    – guiverc
    Feb 18, 2019 at 22:47
  • Same question, although different context: askubuntu.com/q/627781/295286 Effectively duplicate Feb 18, 2019 at 23:30
  • 3
    To prevent the !! from being evaluated (it's being evaluated prior to executing the echo cmd), use quotes, eg echo '!!' gnu.org/software/bash/manual/html_node/Quoting.html
    – michael
    Feb 18, 2019 at 23:33

2 Answers 2

2

!! means the last executed command.

echo !! will echo the last command, which was echo ! in your case.

1

Easily explained:

$ echo 1
1
$ !!
echo 1
1

so !! just repeats the last command, so

echo !!

does excactly what it's supposed to to!

;-)

Not the answer you're looking for? Browse other questions tagged .