Shell exclamation mark command

2019-06-01 00:49发布

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

1条回答
▲ chillily
2楼-- · 2019-06-01 01:21

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.

查看更多
登录 后发表回答