Kosta B.
Kosta B.

Reputation: 271

Shell exclamation mark command

What does this command do?

!g++

For the history command:

!12

It runs command #12 of the history, but what about g++, or another:

!cat filename

Upvotes: 1

Views: 2705

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52102

See the Bash manual, "Event Designators":

!string

Refer to the most recent command preceding the current position in the history list starting with string.

This means that !g++ runs the last command that began with g++, calling the GNU C++ compiler:

$ g++ -o myprog -flto -O3 foo.o bar.o baz.o -lgfortran
...
(g++ does its job here)
...
$ vim test
...
(other commands)
...
$ !g++
g++ -o myprog -flto -O3 foo.o bar.o baz.o -lgfortran  <-- same command as before

!cat filename, on the other hands, doesn't make a lot of sense as it's already a complete command. Unless there was a super complicated pipe after that command the last time, of course, which the event designator would then repeat.

Upvotes: 2

Related Questions